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
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.
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.
// ...
}
Implemented in Kotlin, and using Anko:
val googleSignInAccount = GoogleSignIn.getLastSignedInAccount(act)
if (googleSignInAccount != null) {
getGoogleSignInClient().signOut()
}
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
}
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.