简体   繁体   English

如何检查用户是否已经通过 gmail 或谷歌帐户登录?

[英]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您应该使用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


            }
        }

    });

}

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.

相关问题 如何检查用户是否刚刚在 firebase 中首次登录 Google - How to check if A user just Google signed in for first time in firebase 如何通过JavaMail访问Gmail帐户 - How to access gmail account via javamail 如何使用API​​服务器端将我的帐户中的数据提供给Google登录用户? - How can I use API server side to provide datas from my account to a google signed in user? 如何检查用户是否已经在 google auth 中登录。 在安卓中 - How to check if user is already login in google auth. in android 检查用户是否已登录 Firebase 通过电话号码进行身份验证 - Check if user is already signed in to Firebase Authentication by phone number google oauth2使用user@gmail.com模拟服务帐户 - google oauth2 impersonate service account with user@gmail.com 如何检查登录 Firebase 用户是否有密码 - How to check if signed in Firebase user has a Password 如果我已经登录了多个Gmail帐户,则Google Picker API会强制显示帐户选择器页面 - Google picker api to force to show the account chooser page, if i already logged into the multiple Gmail Accounts 如何通过硒登录谷歌账户 - How to login in Google account via selenium 当我尝试通过我的Google Suite帐户发送电子邮件时,Gmail API返回failedPrecondition错误 - Gmail API returns failedPrecondition error when I try to send an email via my Google Suite Account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM