Google Sign-in removes existing user data

后端 未结 1 1795
萌比男神i
萌比男神i 2021-01-22 16:49

I\'m building an Android app with Firebase Authentication and Cloud Firestore. The app has multiple Auth providers; a user can sign in either using his registered email and pass

1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 17:46

    What I want is The user logs in with his registered email and password and then logs out. When he goes in for login via Google, there needs to be an error toast preventing him from signing in via Google if the same email address has been registered already. Basically, if his email address has been registered, then he can log in only via email and password authentication and not via Google.

    The flow for solving this problem is to ask the user for the email address from the beginning. Once you have the email address you can check if the user has already an account or not. Assuming that you have distinct buttons for each authentication provider you can display or hide them according to what the user has selected for authentication first time. For instance, if the user has selected the authentication with email and password, check that using:

    auth.fetchSignInMethodsForEmail(email).addOnCompleteListener(signInMethodsTask -> {
        if (signInMethodsTask.isSuccessful()) {
            List signInMethods = signInMethodsTask.getResult().getSignInMethods();
            for (String signInMethod : signInMethods) {
                switch (signInMethod) {
                    case GoogleAuthProvider.PROVIDER_ID:
                        googleSignInButton.setVisibility(VISIBLE);
                        facebookSignInButton.setVisibility(GONE);
                        passwordSignInButton.setVisibility(GONE);
                        break;
                    case FacebookAuthProvider.PROVIDER_ID:
                        googleSignInButton.setVisibility(GONE);
                        facebookSignInButton.setVisibility(VISIBLE);
                        passwordSignInButton.setVisibility(GONE);
                        break;
                    case EmailAuthProvider.PROVIDER_ID:
                        googleSignInButton.setVisibility(GONE);
                        facebookSignInButton.setVisibility(GONE);
                        passwordSignInButton.setVisibility(VISIBLE);
                        break;
                    default:
                        googleSignInButton.setVisibility(VISIBLE);
                        facebookSignInButton.setVisibility(VISIBLE);
                        passwordSignInButton.setVisibility(VISIBLE);
                        break;
                }
            }
        } 
    });
    

    In the case of EmailAuthProvider.PROVIDER_ID, hide the other buttons and display only the button that provides the sign-in with email and password. If the user is new, display all buttons so the user can choose one or the other authentication options.

    P.S. There is no need to let the user choose to sign-in with another provider if you only want to let the user sign-in with a particular one.

    0 讨论(0)
提交回复
热议问题