How to create a configuration screen in an Android app that appears only when the user is registered?

0

I'm developing an with Firebase authentication, I need place a screen where the user will define a "nickname", but it must be immutable. Therefore, this screen must appear soon after the registration and only this time, when accessing the same account by another device should not be possible to change the nickname.

I found some things about SharedPreferences , but I did not understand if the data saved is just on the device, and how to do it with Firebase.

    
asked by anonymous 02.08.2017 / 15:40

2 answers

4

As you are using firebase , I do not think you need to use SharedPreferences in this case, as you want to set a nickname strong> immutable if it does not exist, you will only need to apply logic directly using firebase . SharedPreferences is useful in case of saving application configuration (such as themes, colors, splash screens, etc). You can read a bit more about SharedPrefences here:

Basically, you will need to check if the user has a nickname, if he has one, you do not show the nickname entry screen, otherwise you show.

Your node will probably look something like this:

{
    "Users": [
     {
        "userUUID/Key": {
            "name": "Corey",
            "apelido": ""
        }
     }
  ]
}

And the logic applied will be:

DatabaseReference db = FirebaseDatabase.getInstance().getReference("Users");

db.child(db.push().getKey() /* UUID do usuario */).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.child("apelido").exists()) {
            // o apelido já foi definido
        } else {
           // o apelido ainda não foi definido
        }
    }

    @Override
    public void onCancelled(DatabaseError error) {
        Log.e(TAG, error.toException());
    }
});

You will also be able to check if the nickname exists in other instances. What I mean is, if the user terminates the registration but does not set a nickname, you can check if the nickname was set as soon as the application opens, with that same logic, causing the user to come back to set it. This can be when the app is opened or when the user logs in.

    
02.08.2017 / 16:08
2

Hello. SharedPreferences is not a good option because the data is stored on the device itself. If the user logs in to another device, this data will not be available. The ideal is to save this information in the Firebase itself, preferably in the Database. Create a user node and use the user's UID as the key: user / 6rh7zl2aZXhAkUDlQdIjVLCx61r2 / ... Within this node you save your user object: name, email, nickname , etc. Please read and write to Firebase Database: link When logging into your app successfully, you get a UID through firebaseAuth. With this UID you retrieve the recorded object and go to the main screen of your app. If firebaseAuth is null, you redirect to the log screen. I hope I have helped.

    
02.08.2017 / 15:56