android – How to re-authenticate a user on Firebase with Google Provider?-ThrowExceptions

Exception or error:

The example for using reauthenticate() in Firebase shows only how to re-authenticate a user who signed with Email and Password:

AuthCredential credential = EmailAuthProvider.getCredential("user@example.com", "password1234");
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential);

I also know how to re-authenticate with Facebook Provider (credential = FacebookAuthProvider.getCredential(AccessToken.getCurrentAccessToken().toString())).

Problem is there’s no equivelant method in Google API to get the current Access Token and eventually get the AuthCredential. So what do I pass to getCredential() in this case?

How to solve:

I know this is old question but I did not found complete answer to this. This is how to do it on Android.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
// Get the account
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(context);
if (acct != null) {
     AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
     user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "Reauthenticated.");
            }
        }
     });
} 

###

Considering you would have received GoogleSignInResult as a response to sign-in, I think you can use the following code:

// assuming result variable has GoogleSignInResult
// GoogleSignInResult result 

// Get the account
GoogleSignInAccount acct = result.getSignInAccount();

// credential
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {...

###

You can get GoogleSignInResult via 2 way to authenticate.

i) By entering email id and password into google login screen.

ii) By selecting account from already logged in account in phone.

i have used 2nd approach to get access token and authenticate user.

for more support references links are given below.

google sign in link 1

Stackoverflow – token refresh

google auth provider documentation

server side token verification docs

if your only objective is, to get token so you can also try this github source.

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestEmail()
    .build();

//use sign in option to build api client instence.

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN); }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from    GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN){
    GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data);
    handleSignInResult(result);
    }


private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
    // Signed in successfully, show authenticated UI.
    GoogleSignInAccount acct = result.getSignInAccount();


} else {
    // Signed out, show unauthenticated.

       }
}

// get authenticated

AuthCredential credential =
GoogleAuthProvider.getCredential(acct.getIdToken(), null);
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
// add your job here on authenticated
} 
// if token is obsoleted then you can do this
credential.refreshToken();
accessToken = credential.getAccessToken();

Leave a Reply

Your email address will not be published. Required fields are marked *