Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Sign In: check if User is signed in

Tags:

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

like image 409
Maxence Duthoo Avatar asked Jul 04 '16 18:07

Maxence Duthoo


People also ask

What is GoogleSignInClient?

public class GoogleSignInClient extends GoogleApi<GoogleSignInOptions> A client for interacting with the Google Sign In API.


1 Answers

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.

like image 187
Linh Avatar answered Oct 12 '22 01:10

Linh