26

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?

Heath
  • 185
  • 1
  • 9
Alaa M.
  • 4,961
  • 10
  • 54
  • 95

3 Answers3

8

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.");
            }
        }
     });
} 
kivmii
  • 178
  • 2
  • 10
  • 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. Jul 04 '20 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. – user3421469 Jul 26 '20 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>() {...
manishg
  • 9,520
  • 1
  • 16
  • 19
  • 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. Mar 21 '17 at 06:07
0

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();
Community
  • 1
  • 1