java - 如何检查用户是否已通过 gmail 或 google 帐户登录?
问题描述
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 或电子邮件和密码登录,请使用以下代码:
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
}
}
因此,您可以根据用户登录的方式采取一些措施。
推荐阅读
- ruby-on-rails - 使用 Elastic Search gem 运行 jruby 应用程序。
- python - __new__() 缺少 1 个必需的位置参数,具体取决于抓取的 url
- java - Java 中的 SIP URI 验证
- html - 将 Django Web 应用程序格式化为 1024x768
- c# - 使用 EPPLUS 在 for 循环中写入单元格会导致重复值
- swagger - Swagger YAML 文件添加动态 URL
- python - 如何映射多列python
- r - 带有几个数字的 Rstudio 维恩图
- android - 如何在应用程序关闭时向 Android 发送推送通知
- javascript - 为什么我的 SQL 代码中出现语法错误?