使用createUserWithEmailAndPassword进行Android Firebase身份验证
我正试图通过Android向Firebase添加一个新用户,但我收到了一条对我来说毫无意义的错误消息。我已在Firebase控制台中启用电子邮件/密码登录。我收到的错误消息显示: com.example.myproject D/RegisterActivity:validateForm:LoginActivity validateForm已启动。 com.example.myproject I/BiChannelGoogleApi:[FirebaseAuth:]GetGoogleAPI方法() 返回的Gms:com.google.firebase.auth.api.internal。zzao@xyz123 com.example.myproject W/FirebaseMethods: createUserWithEmail:失败 com.google.firebase.FirebaseException:发生内部错误。[7:] 我的代码库如下:使用createUserWithEmailAndPassword进行Android Firebase身份验证,android,firebase,firebase-authentication,Android,Firebase,Firebase Authentication,我正试图通过Android向Firebase添加一个新用户,但我收到了一条对我来说毫无意义的错误消息。我已在Firebase控制台中启用电子邮件/密码登录。我收到的错误消息显示: com.example.myproject D/RegisterActivity:validateForm:LoginActivity validateForm已启动。 com.example.myproject I/BiChannelGoogleApi:[FirebaseAuth:]GetGoogleAPI方法()
public class FirebaseMethods {
private static final String TAG = "FirebaseMethods";
// declare Firebase auth
private FirebaseAuth mAuth;
private String user;
private Context mContext;
public FirebaseMethods(Context context){
mAuth = FirebaseAuth.getInstance();
mContext = context;
if(mAuth.getCurrentUser() != null){
user = mAuth.getCurrentUser().getUid();
}
}
/**
* Register a new username and email to Firebase authentication
* @param email
* @param password
* @param username
*/
public void registerNewEmail(String email, String password, String username){
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(mContext, "Authentication failed.", Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
private void updateUI(FirebaseUser user) {
Log.d(TAG, "updateUI: LoginActivity updateUI started.");
if (user != null) {
/*If the user is already logged in then navigate back to the MainActivity.*/
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
} else {
//add alternative action here
}
}
公共类FirebaseMethods{
私有静态最终字符串标记=“FirebaseMethods”;
//声明Firebase身份验证
私人消防队;
私有字符串用户;
私有上下文;
公共FirebaseMethods(上下文){
mAuth=FirebaseAuth.getInstance();
mContext=上下文;
if(mAuth.getCurrentUser()!=null){
user=mAuth.getCurrentUser().getUid();
}
}
/**
*注册新用户名并发送电子邮件至Firebase身份验证
*@param电子邮件
*@param密码
*@param用户名
*/
公共无效注册表电子邮件(字符串电子邮件、字符串密码、字符串用户名){
mAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(新的OnCompleteListener)仅在firebase控制台中启用电子邮件/密码登录是不够的
你必须做到以下几点
转到android studio中的“工具”:
选择Firebase:它将在窗口右侧显示提示
选择身份验证->电子邮件和密码身份验证
连接到firebase
它将显示一个对话框窗口。->选择要连接应用程序的现有项目。如果尚未使用android studio登录firebase,请使用启用电子邮件/密码验证的电子邮件登录
在此之后,单击“将FIREBASE身份验证添加到您的应用程序”,它将再次显示一个对话框窗口。然后单击“接受更改”。它将自动添加一些更改
将依赖项和google-services.json文件添加到项目中
在android studio中完成配置
注意:在创建用户时以正确的方式添加您的电子邮件地址。例如:'xyz@gmail.com“。有时由于电子邮件地址格式不正确而显示错误
现在在该项目中使用您的登录方法并尝试再次登录。它肯定会起作用。您是否从fire base console打开了身份验证
?我已打开它。请参阅更新的照片。我认为问题在我的代码中。我已打开它。请参阅更新的照片。我认为问题在我的代码中。