简体   繁体   English

如何在Firebase中添加带有电子邮件+密码身份验证的DisplayName? Android的

[英]How to add DisplayName with email + password authentication in Firebase? Android

private void registerUser(){
String emailId = email.getText().toString().trim().toLowerCase();
String password = pass.getText().toString().trim();

firebaseAuth.createUserWithEmailAndPassword(emailId,password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                progressDialog.dismiss();
                if(task.isSuccessful()){
                    Toast.makeText(MainActivity.this,"Registration Successful",Toast.LENGTH_SHORT).show();

                    //show chatroom
                    finish();
                    startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
                }
                else{
                    Toast.makeText(MainActivity.this,"Registration Failed. Please try again",Toast.LENGTH_SHORT).show();
                }
            }
        });
}

I wish to add a username or display name to it but am unable to do so. 我希望为其添加用户名或显示名称,但我无法这样做。 I tried a few things but still no result. 我尝试了一些但仍然没有结果。 Kindly help me. 请帮助我。 I need this functionality for a project submission this week. 我本周需要为项目提交提供此功能。

This is definitely possibly but just not in the user creation method. 这肯定是可能的,但不是在用户创建方法中。

Once you've created your user (possibly in the addOnSuccessListener ) you can use something similar to the following code to update the Users DisplayName: 一旦您创建了用户(可能在addOnSuccessListener ),您就可以使用类似于以下代码的内容来更新Users DisplayName:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder().setDisplayName("John Smith").build();

user.updateProfile(profileUpdates);

Hope this helps! 希望这可以帮助!

Edit: I previously said to add the code to the AuthStateListener, however, Frank's suggestion below to put it in the addOnSuccessListener is better/makes more sense so I have updated the answer to reflect this. 编辑:我之前说过要将代码添加到AuthStateListener中,但是,Frank下面的建议将它放在addOnSuccessListener中更好/更有意义,所以我更新了答案以反映这一点。

I just recently investigated this issue for my own implementation (SDK version 4.4.1). 我刚刚为自己的实现调查了这个问题(SDK版本4.4.1)。 What I've found is that it works perfectly if you are sure to utilize the exact same task.result object from registration/login and not the object from the default instance. 我发现如果您确定使用来自注册/登录的完全相同的task.result对象而不是默认实例中的对象,它将完美地运行。

Another work around that helped me is to have an email reference table in your FB DB like this: 另一个帮助我的工作是在FB DB中有一个电子邮件参考表,如下所示:

{ "EmailRef": { "username1" : "email@ domain .com"}, {"username2" : "email2@domain.com"} }

And then to query for the username by the user's email (from auth.CurrentUser.Email) using a method like this: 然后使用如下方法通过用户的电子邮件(来自auth.CurrentUser.Email)查询用户名:

public static void GetCurrentUserName(Firebase.Auth.FirebaseUser user)
{
    string message = "";
    DatabaseReference dbRef = FbDbConnection();
    FirebaseDatabase.DefaultInstance.GetReference("EmailRef").OrderByValue().EqualTo(user.Email).GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            message = "GetCurrentUserName encountered an error: " + task.Exception;
            ModalManager.BuildFireBaseDebugModal(message);
            Debug.LogError(message);
            return;
        }
        if (task.IsCanceled)
        {
            message = "GetCurrentUserName was canceled.";
            Debug.LogError(message);
            return;
        }
        if (task.IsCompleted)
        {
            foreach (DataSnapshot ss in task.Result.Children.AsEnumerable())
            {
                try
                {
                    if (ss.Value != null)
                    {
                        if (ss.Value.ToString() == user.Email)
                        {
                            message = "GetCurrentUserName was successful -- Email: " + user.Email + " Username: " + user.DisplayName;
                            Debug.LogError(message);
                        }
                    }
                    return;
                }
                catch (Exception ex)
                {
                    message = "GetCurrentUserName Exception: " + ex;
                    Debug.LogError(message);
                    return;
                }
            }
        }

    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Firebase电子邮件/密码身份验证获取UID并添加用户信息 - How to get UID and add user information with Firebase email/password authentication Firebase身份验证API电子邮件/密码Android - Firebase Authentication API Email/Password Android Android Firebase 电子邮件和密码身份验证不起作用 - Android Firebase Email and password authentication does not work Android-Firebase身份验证不适用于电子邮件/密码设置 - Android - Firebase Authentication not working with Email/Password setup 使用电子邮件和密码进行 Firebase 身份验证 - Firebase Authentication With Email & Password 如何将名称以及电子邮件和密码添加到Firebase数据库Android - How to add Name along with email and password to firebase database android 如何绑定Email的手机和Firebase的密码认证? - How to link Phone with Email and Password Authentication in Firebase? 使用Firebase身份验证通过电子邮件/密码将Firebase连接到Android应用程序 - Connecting Firebase to Android application using Firebase authentication via email/password 如何将 Firebase 电话身份验证与电子邮件/密码身份验证联系起来? - How Can I Link Firebase Phone Authentication with Email/Password Authentication? Firebase 电子邮件和密码身份验证失败 - Firebase email and password authentication fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM