如何在Firebase中通过电子邮件+密码验证添加DisplayName? Android的 [英] How to add DisplayName with email + password authentication in Firebase? Android

查看:293
本文介绍了如何在Firebase中通过电子邮件+密码验证添加DisplayName? 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>任务){
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();
}
}
} );

$ / code>

我想添加一个用户名或显示名称,但无法执行所以。我尝试了一些东西,但仍然没有结果。请帮助我。本周我需要这个项目提交功能。

解决方案

这绝对是可能的,但不是用户创建方法。 / p>

一旦你创建了你的用户(可能在 addOnSuccessListener 中),你可以使用类似下面的代码更新用户显示名称:

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

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

user.updateProfile(profileUpdates);

希望这有助于您!

编辑:我之前说过要将代码添加到AuthStateListener中,但是,Frank的建议将其放在addOnSuccessListener中更好/更有意义,所以我已经更新了答案以反映这一点。


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:

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.

这篇关于如何在Firebase中通过电子邮件+密码验证添加DisplayName? Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆