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 anOptionalPendingResult
object which is used to check whether the credential is valid or not. If the credential is validOptionalPendingResult
'sisDone()
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/OptionalPendingResultAndroid Documentation for
GoogleSignInApi
: https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApiHere's the code for checking if the credentials are valid or not.