Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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("[email protected]", "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?

like image 569
Alaa M. Avatar asked Sep 07 '25 10:09

Alaa M.


1 Answers

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.");
            }
        }
     });
} 
like image 135
kivmii Avatar answered Sep 09 '25 01:09

kivmii