Я использую Firebase для аутентификации пользователя по электронной почте и паролю, и я хочу получить имя пользователя из authData, но я вижу только uid. Я хочу получить имя пользователя, чтобы отобразить его в другом действии.

Вот мой код:

      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 map = new HashMap();
                    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);
                }
            });
        }
    });`

, и результатом будет результат из приведенного выше кода:

this is the result of the code from above

Спасибо.

Ответы (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.

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://.firebaseio.com");
ref.createUser(email.getText().toString(), password.getText().toString(), new Firebase.ValueResultHandler>() {
    @Override
    public void onSuccess(Map result) {
        Map map = new HashMap();
        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 map = new HashMap();
                    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);

                }

2022 WebDevInsider