20

I am looking for a way to check if my user already signed in with Google Sign In.

I support several logging APIs (Facebook, Google, custom), so I would like to build a static helper method like: User.isUserLoggedIn()

With Facebook I use:

if AccessToken.getCurrentAccessToken() != null { 
   return true
} 

to check if the user is logged via Facebook.

On iOS I use the following to check if the user is logged via Google Sign In:

GIDSignIn.sharedInstance().hasAuthInKeychain()

My question: Is there an equivalent on Android to the iOS method :

GIDSignIn.sharedInstance().hasAuthInKeychain() ?

I am looking for a method that doesn’t involve a callback.

Thanks! Max

Maxence Duthoo
  • 201
  • 1
  • 2
  • 4

4 Answers4

24

You can use this function

private boolean isSignedIn() {
  return GoogleSignIn.getLastSignedInAccount(context) != null;
}

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignIn

public static GoogleSignInAccount getLastSignedInAccount (Context context)

Gets the last account that the user signed in with.

Returns: GoogleSignInAccount from last known successful sign-in. If user has never signed in before or has signed out / revoked access, null is returned.

Linh
  • 57,942
  • 23
  • 262
  • 279
  • 2
    it's one thing that you check if the user is logged, but for example if the user removes your app from his google account manually on google web (myaccount.google.com/permissions), the isSignedIn function still says you are logged, because you are, but you can't perform any operations with google inside your app, as the app was removed from your accound manually. So you shoul also check GoogleSignIn.hasPermissions() – Darksymphony Nov 14 '20 at 17:45
  • `GoogleSignIn.hasPermissions() ` also says that the user is logged in even if I remove the app from Google Cloud. My recommendation is to perform some operation and check for [`UserRecoverableAuthIOException`](https://cloud.google.com/java/docs/reference/google-api-client/latest/com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException), it is thrown when the user doesn't have permissions. Then call something like `myActivity.startActivityForResult( userRecoverableException.getIntent(), REQUEST_AUTHORIZATION);` to obtain required permissions again. – Vít Kapitola Oct 12 '21 at 07:18
  • this still return not null even after I have called `SignInClient.signOut()` – Ricky Mo Mar 27 '22 at 16:09
8

Take a look at the Android sign-in documentation:

To check if the user is signed-in, call isConnected():

if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
   // signed in. Show the "sign out" button and explanation.
   // ...
} else {
   // not signed in. Show the "sign in" button and explanation.
   // ...
}
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Dopaz
  • 130
  • 2
  • 5
2

Implemented in Kotlin, and using Anko:

    val googleSignInAccount = GoogleSignIn.getLastSignedInAccount(act)
    if (googleSignInAccount != null) {
        getGoogleSignInClient().signOut()
    }
Swapnil
  • 344
  • 4
  • 5
1

In Kotlin!

To check the sign-in via Google you need to check both firebase auth and getLastSignedInAccount.

private lateinit var mauth : FirebaseAuth
mauth = FirebaseAuth.getInstance()
val user = mauth.currentUser


if (user != null && isSignedIn()){           
    //Logic
}

private fun isLoggedIn(): Boolean {
    return GoogleSignIn.getLastSignedInAccount(context) != null
}
Lubna mariyam
  • 326
  • 2
  • 5