简体   繁体   中英

How to check if a user has logged in with Google Account

I am integrating Facebook and Google authentication in my android application. While launching the application, I want to check if a user is logged on to the app with Facebook or Google authentication. I got success with Facebook using the below code:

if (Profile.getCurrentProfile() != null && AccessToken.getCurrentAccessToken() != null){
        Intent i = new Intent(Splash.this, SecondActivity.class);
        startActivity(i);
        finish();
}

But having no success with Google. Also, I searched for many answers but most of them were using Firebase for Google authentication.

How would I achieve this using Google Authentication and not Firebase.

Help would be appreciated. Thanks in advance!

We can use GoogleSignInApi.silentSignIn() method to check if the login credential is valid or not. It returns an OptionalPendingResult object which is used to check whether the credential is valid or not. If the credential is valid OptionalPendingResult 's isDone() method will return true. The get method can then be used to obtain the result immediately (If it is available).

Android Documentation for OptionalPendingResult : https://developers.google.com/android/reference/com/google/android/gms/common/api/OptionalPendingResult

Android Documentation for GoogleSignInApi : https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApi

Here's the code for checking if the credentials are valid or not.

OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(google_api_client);
if (opr.isDone()) {
   // If the user's cached credentials are valid, the 
   // OptionalPendingResult will be "done" and the 
   // GoogleSignInResult will be available instantly.
   Log.d("TAG", "Got cached sign-in");

   GoogleSignInResult result = opr.get();

   handleSignInResult(result);
}

I have been puzzled over this problem and couldnt find an appropriate solution for a long time as well. The solution turns out to be short:

String strProvider = FirebaseAuth.getInstance().
         getAccessToken(false).getResult().getSignInProvider();

So, if (strProvider.equals("password")) then the authentication is by Email + Password,
if (strProvider.equals("google.com")) then the authentication is via Google,
if (strProvider.equals("facebook.com")) then the authentication is via Facebook.

Addition

However, with this one-liner you can get an exception wchich can be prevented by adding OnSuccessListener like so:

mAuth = FirebaseAuth.getInstance();
mAuth.getAccessToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
                @Override
                public void onSuccess(GetTokenResult getTokenResult) {
                    strProvider = getTokenResult.getSignInProvider();
                }
            });

Easiest way:

 for (UserInfo user : auth.getCurrentUser().getProviderData()) {
        if (user.getProviderId().contains("facebook.com")) IS_FACEBOOK_LOGIN = true;
        else if (user.getProviderId().contains("google.com")) IS_GOOGLE_LOGIN = true;
    }
  @Override
    public void onStart() {
        super.onStart();

        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
            // and the GoogleSignInResult will be available instantly.
            Log.d(TAG, "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            // If the user has not previously signed in on this device or the sign-in has expired,
            // this asynchronous branch will attempt to sign in the user silently.  Cross-device
            // single sign-on will occur in this branch.
            showProgressDialog();
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    hideProgressDialog();
                    handleSignInResult(googleSignInResult);
                }
            });
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM