[英]How to check if user is already signed in via gmail or google account?
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();
}
}
}
});
}
The Problem I am facing is:我面临的问题是:
The user first signup using 'signup with email'.用户首先使用“使用电子邮件注册”进行注册。 After that the user signup with a Google account(With the same email).
之后,用户使用 Google 帐户注册(使用相同的电子邮件)。
Now after that when the user tries to login via email and password.现在,当用户尝试通过 email 和密码登录时。 I get the error that the password is incorrect, I know this happens because sign up with email details has been overwritten by Google account details.
我收到密码不正确的错误,我知道这是因为使用 email 注册的详细信息已被 Google 帐户详细信息覆盖。
I want to show the error "The email is linked with google account. Please login via google account".我想显示错误“email 与 google 帐户相关联。请通过 google 帐户登录”。
You should use AuthCredential
and isNewUser
Here is the example您应该使用
AuthCredential
和isNewUser
这是示例
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
}
}
});
}
To know if a user is signed in with Google or email and password, please use the following code:要了解用户是否使用 Google 或 email 和密码登录,请使用以下代码:
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
List<? extends UserInfo> userInfoList = firebaseUser.getProviderData();
for (UserInfo userInfo : userInfoList) {
String providerId = userInfo.getProviderId();
if (providerId.equals(GoogleAuthProvider.PROVIDER_ID)) {
//Do stuff
} else if (providerId.equals("password")) {
//Do other stuff
}
}
So you can take some action according to the way the user is signed in.因此,您可以根据用户登录的方式采取一些措施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.