Android FireBase Auth createUserWithEmailAndPassword()。then()不工作
我正在开发一个android应用程序,其中包括用户firebase auth,用于用户注册。注册工作正常,但我想通过在.then()中实现将用户名添加到数据库中,但我的android studio一直给出“unresolved method then(?)”。我也在使用catch,但似乎效果不错。我正在编写的代码如下,其中firebaseAuth是firebaseAuth类型的对象:Android FireBase Auth createUserWithEmailAndPassword()。then()不工作,android,firebase-authentication,Android,Firebase Authentication,我正在开发一个android应用程序,其中包括用户firebase auth,用于用户注册。注册工作正常,但我想通过在.then()中实现将用户名添加到数据库中,但我的android studio一直给出“unresolved method then(?)”。我也在使用catch,但似乎效果不错。我正在编写的代码如下,其中firebaseAuth是firebaseAuth类型的对象: firebaseAuth.createUserWithEmailAndPassword(
firebaseAuth.createUserWithEmailAndPassword(email,password)
.then( (u) => {
//Implementation of then
})
.catch(error => {
switch (error.code) {
case 'auth/email-already-in-use':
EmailWarning.setText("Email already in use");
case 'auth/invalid-email':
EmailWarning.setText("Invalid Email");
case 'auth/weak-password':
PasswordWarning.setText("Password should be 8 characters or longer");
default:
PasswordWarning.setText("Error during sign up");
}
});
我在下面发现了一个类似的问题,但即使在尝试了这个之后,它仍然不起作用
我进一步查看了firebase文档,发现了另一个在complete listener上使用的实现,但是本文中描述的错误代码似乎不适用于它
更新1:
我最终在complete listener上实现了以下功能:
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, 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
FirebaseUser user = firebaseAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(Username)
.build();
user.updateProfile(profileUpdates);
}
else {
// If sign up fails, display a message to the user.
switch (task.getException()) {
case "auth/email-already-in-use":
EmailWarning.setText("Email already in use");
case "auth/invalid-email":
EmailWarning.setText("Invalid Email");
case "auth/weak-password":
PasswordWarning.setText("Password should be 8 characters or longer");
default:
PasswordWarning.setText("Error during sign up");
}
}
}
});
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, 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
FirebaseUser user = firebaseAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(Username)
.build();
user.updateProfile(profileUpdates);
OpenApp();
}
else {
// If sign up fails, display a message to the user.
String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();
if (task.getException() instanceof FirebaseAuthUserCollisionException)
EmailWarning.setText("Email already in use");
else if(task.getException() instanceof FirebaseAuthInvalidCredentialsException)
EmailWarning.setText("Invalid Email");
else if(task.getException() instanceof FirebaseAuthWeakPasswordException)
PasswordWarning.setText("Password should be 8 characters or longer");
else
PasswordWarning.setText("Error during sign up");
}
}
});
firebaseAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
FirebaseUser=firebaseAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates=新建UserProfileChangeRequest.Builder()
.setDisplayName(用户名)
.build();
user.updateProfile(profileUpdates);
}
否则{
//如果注册失败,则向用户显示消息。
开关(task.getException()){
案例“身份验证/电子邮件已在使用”:
EmailWarning.setText(“电子邮件已在使用”);
案例“验证/无效电子邮件”:
EmailWarning.setText(“无效电子邮件”);
案例“身份验证/弱密码”:
PasswordWarning.setText(“密码应为8个字符或更长”);
违约:
PasswordWarning.setText(“注册时出错”);
}
}
}
});
现在,用户添加部件工作正常,但异常处理程序无法处理字符串,因此我无法找到处理firebase文档中给出的错误代码的方法
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, 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(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
firebaseAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是一个新的OnCompleteListener,我无法找到.then()问题的解决方案,但在深入搜索getExceptions()后,我在此处找到了一个解决方案。现在,我当前的实现如下所示:
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, 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
FirebaseUser user = firebaseAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(Username)
.build();
user.updateProfile(profileUpdates);
}
else {
// If sign up fails, display a message to the user.
switch (task.getException()) {
case "auth/email-already-in-use":
EmailWarning.setText("Email already in use");
case "auth/invalid-email":
EmailWarning.setText("Invalid Email");
case "auth/weak-password":
PasswordWarning.setText("Password should be 8 characters or longer");
default:
PasswordWarning.setText("Error during sign up");
}
}
}
});
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, 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
FirebaseUser user = firebaseAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(Username)
.build();
user.updateProfile(profileUpdates);
OpenApp();
}
else {
// If sign up fails, display a message to the user.
String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();
if (task.getException() instanceof FirebaseAuthUserCollisionException)
EmailWarning.setText("Email already in use");
else if(task.getException() instanceof FirebaseAuthInvalidCredentialsException)
EmailWarning.setText("Invalid Email");
else if(task.getException() instanceof FirebaseAuthWeakPasswordException)
PasswordWarning.setText("Password should be 8 characters or longer");
else
PasswordWarning.setText("Error during sign up");
}
}
});
firebaseAuth.createUserWithEmailAndPassword(电子邮件,密码)
.addOnCompleteListener(这是新的OnCompleteListener。我尝试了这一个,我能够解决用户名添加问题,但是其他firebase文档(我在catch中使用)中描述的错误代码它不起作用,或者我无法为它找出正确的条件格式如何告诉我你尝试了什么,你可以用附加信息编辑你的帖子好的,我添加了新的实现我想你需要task.getError()
,我不认为task.getException()
存在->和isFinished()
而不是issusccessful()
-isFinished()
只有在没有错误的情况下才能从上面的同一文档链接调用。我会记录task.getError()
以确保您知道字符串是什么,您可能只会进入错误块,因为issusccessful()
不是函数。task.getError()
将返回一个Throwable
->-看起来您可以使用getMessage()
或toString()
来获取错误的字符串表示形式。要获取实际的Throwable
可以使用getCause()
-这将返回一个异常对象,如InitializationException
,如果您知道哪些异常是可能的,您可以对这些异常使用=
。