Firebase: write database before logging in

I am using Firebase in my android app. My goal is that the user of the application should be able to write data to the database, but I do not want the user to register, so I use anonymous login.

I have a form that the user can submit and will be bound to the database. At the time of sending, it is possible that anonymous login cannot be performed. Is it possible that in Firebase I call the write statement to the database and it writes locally and exits when the user signs anonymously?

I know that Firebase provides standalone capabilities. But I guess that if I were to write a database for writing before logging in, it would give me an error.

+4
source share
1 answer

I had the same problem when developing an application. If the user had his phone on the plane or was disconnected at the first start, they may have reached the point where they needed to save the data before successful anonymous authentication.

" " , , . , , , . , , Firebase , .

, push , . SharedPreferences .

    db = FirebaseDatabase.getInstance();
    db.setPersistenceEnabled(true);

    dbRef = db.getReference();

    // This key should be saved on the user device until authenticated
    String key = dbRef.child("unauthenticated").push().getKey();

    // Write some data 
    dbRef.child("unauthenticated").child(key)
         .child("somedata").setValue("testing");

, , , . ...

    dbRef.child("unauthenticated").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("DB", "Got value from database: " + dataSnapshot.child("somedata").getValue());

            if(FirebaseAuth.getInstance().getCurrentUser() != null) {
                /** If user is authenticated, move data to authenticated location **/
            }
        }
        @Override public void onCancelled(DatabaseError databaseError) {}
    });
0

Source: https://habr.com/ru/post/1660632/


All Articles