28

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?

4 Answers 4

11

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.");
            }
        }
     });
} 
2
  • 1
    4 years later I'm back to the same issue and looks like this is working. So I'm choosing it as an answer! Thanks!
    – Alaa M.
    Commented Jul 4, 2020 at 14:00
  • The thing is that the token expires after 1 hour. I don't think there is a way in Android to get a refresh token and re-authenticate the user with it. Commented Jul 26, 2020 at 22:03
0

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>() {...
1
  • I don't remember what I ended up doing (it's 4 months ago), but I think I needed something static that I could use whenever I want and not just on sign-in (for example if user wanted to delete their account). Anyway, because I'm not working on this anymore, if someone believe this is the correct answer I'll choose it as an answer.
    – Alaa M.
    Commented Mar 21, 2017 at 6:07
0

The updated way to do it is by using

startActivityForReauthenticateWithProvider

He is an example:

suspend fun reAuthenticateUserGoogle(activity: Activity) {
    val provider = OAuthProvider.newBuilder("google.com")

    firebaseAuth.currentUser?.startActivityForReauthenticateWithProvider(activity, provider.build())
                .addOnSuccessListener {
                    // Reauthenticated successfully
                }
                .addOnFailureListener {
                    // Error
                }
}
-1

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.