gpt4 book ai didi

java - 如何检查用户是否已通过 gmail 或 google 帐户登录?

转载 作者:行者123 更新时间:2023-12-01 16:23:53 24 4
gpt4 key购买 nike

                if(!em.isEmpty() || !pass.isEmpty()) {
mfirebaseAuth.signInWithEmailAndPassword(em, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Logged in successfully", Toast.LENGTH_SHORT).show();


} else {
String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();

switch (errorCode) {

case "ERROR_INVALID_EMAIL":
Toast.makeText(MainLoginActivity.this, "The email address is badly formatted.", Toast.LENGTH_LONG).show();
email.setError("The email address is badly formatted.");
email.requestFocus();
break;


case "ERROR_USER_MISMATCH":
Toast.makeText(MainLoginActivity.this, "The supplied credentials do not correspond to the previously signed in user.", Toast.LENGTH_LONG).show();
break;

case "ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL":
Toast.makeText(MainLoginActivity.this, "An account already exists with the same email address but different sign-in credentials. Sign in using associated email address.", Toast.LENGTH_LONG).show();
break;

case "ERROR_EMAIL_ALREADY_IN_USE":
Toast.makeText(MainLoginActivity.this, "The email address is already in use by another account.", Toast.LENGTH_LONG).show();
email.requestFocus();
break;

case "ERROR_CREDENTIAL_ALREADY_IN_USE":
Toast.makeText(MainLoginActivity.this, "This credential is already associated with a different user account.", Toast.LENGTH_LONG).show();
break;

case "ERROR_USER_DISABLED":
Toast.makeText(MainLoginActivity.this, "The user account has been disabled by an administrator.", Toast.LENGTH_LONG).show();
break;

case "ERROR_INVALID_USER_TOKEN":
Toast.makeText(MainLoginActivity.this, "The user\\'s credential is no longer valid. The user must sign in again.", Toast.LENGTH_LONG).show();
break;

case "ERROR_WRONG_PASSWORD":
Toast.makeText(MainLoginActivity.this, "The password is invalid or the user does not have a password.", Toast.LENGTH_LONG).show();
password.setError("password is incorrect ");
password.requestFocus();
password.setText("");
break;

default:
Toast.makeText(getApplicationContext(), "Error! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();


}
}
}
});
}

我面临的问题是:

用户首次使用“通过电子邮件注册”进行注册。之后,用户使用 Google 帐户注册(使用同一电子邮件)。

之后,当用户尝试通过电子邮件和密码登录时。我收到密码不正确的错误,我知道发生这种情况是因为使用电子邮件详细信息进行注册已被 Google 帐户详细信息覆盖。

我想显示错误“该电子邮件已与 Google 帐户关联。请通过 Google 帐户登录”。

最佳答案

您应该使用AuthCredentialisNewUser这是示例

 private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("TAG", "signInWithCredential:success" + acct.getId());

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();
if (isNewUser) {

Log.d(TAG, "Is New User!");
FirebaseUser user = mAuth.getCurrentUser();
//do something
//You can call register class or function here


} else {

Log.d(TAG, "Is Old User!");
//Do something else


}
}

});

}

关于java - 如何检查用户是否已通过 gmail 或 google 帐户登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62198566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com