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

断了今生、忘了曾经 提交于 2019-12-11 06:41:20

问题


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!


回答1:


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);
}



回答2:


  @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);
                }
            });
        }
    }


来源:https://stackoverflow.com/questions/46763614/how-to-check-if-a-user-has-logged-in-with-google-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!