1

I am using Firebase for user authentication through email and password and I want to retrieve the username of a user from authData but I only see the uid. I want to retrieve the username so I can display it on another activity.

Here is my code:

      button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ref.authWithPassword(email.getText().toString(), password.getText().toString(), new Firebase.AuthResultHandler() {
                @Override
                public void onAuthenticated(AuthData authData) {
                    setContentView(R.layout.activity_user_display);
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("provider", authData.getProvider());
                    if(authData.getProviderData().containsKey("displayName")){
                        map.put("displayName", authData.getProviderData().get("displayName").toString());
                    }
                    else
                    {
                        Context c = getApplicationContext();
                        Toast t = Toast.makeText(c, "You thought you had it", Toast.LENGTH_SHORT);
                    }
                    ref.child("users").child(authData.getUid()).setValue(map);

                }

                @Override
                public void onAuthenticationError(FirebaseError firebaseError) {
                    Context c = getApplicationContext();
                    Toast error = Toast.makeText(c,"Sorry :)",Toast.LENGTH_SHORT);
                }
            });
        }
    });`

and the result is this the result from the code above:

this is the result of the code from above

Thank you.

elishaolade
  • 75
  • 12

2 Answers2

2

Probably, you should create a separate DB to store Username of the clients. Since the uid stores all the info of that specific client, u probably need a separate DB.

This might help you :

About Authentication using email

About Auth() Variable

Similar Question for retrieving email in FireBase

Hope it helped you.

Community
  • 1
  • 1
Ajithesh N
  • 66
  • 9
1

When you use email&password authentication, Firebase only requires an email address and corresponding password to authenticate the user. Unlike with the social providers (Google, Facebook, Twitter, Github), there are no additional fields that Firebase looks up in the underlying provider. See the documentation for the properties that are available in authData for the email+password provider.

You can easily add a displayName field to your sign-up form, but you will have to track that yourself and store it in the database with a different code path. E.g.

Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.createUser(email.getText().toString(), password.getText().toString(), new Firebase.ValueResultHandler<Map<String, Object>>() {
    @Override
    public void onSuccess(Map<String, Object> result) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("provider", authData.getProvider());
        if(authData.getProviderData().containsKey("displayName")){
            map.put("displayName", authData.getProviderData().get("displayName").toString());
        }
        else {
            // read the display name from the text field
            map.put("displayName", displayName.getText().toString);
        }
        ref.child("users").child(authData.getUid()).setValue(map);
    }
    @Override
    public void onError(FirebaseError firebaseError) {
        // there was an error
    }
});

Then in your sign-in flow, just make sure to not overwrite the displayName by using updateChildren()

      button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ref.authWithPassword(email.getText().toString(), password.getText().toString(), new Firebase.AuthResultHandler() {
                @Override
                public void onAuthenticated(AuthData authData) {
                    setContentView(R.layout.activity_user_display);
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("provider", authData.getProvider());
                    if(authData.getProviderData().containsKey("displayName")){
                        map.put("displayName", authData.getProviderData().get("displayName").toString());
                    }

                    ref.child("users").child(authData.getUid()).updateChildren(map);

                }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for answering my question. From my understanding, `AuthData` is returned when logging in, so how can the methods for `AuthData` work without an `AuthData` object returned in the sign up process? – elishaolade Jan 02 '16 at 09:31