Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reauthenticating Firebase user with Google Provider in React

I'm using firebase for my web project, and want to allow users to delete their account, which requires reauth according to Firebase. I've been having a lot of trouble getting their user.reauthenticateAndRetrieveDataWithCredential to work.

Specifically, I'm not sure how to generate an AuthCredential to pass to it using the GoogleAuthProvider.credential() method, since I don't know how to get a user's ID token or access token.

I've tried user.getIdToken() (here) which gives me an error that the credential is not issued by Google.

Asking the user to sign in again with a popup and then taking the res.credential accessToken or idToken works, but then allows them to sign in to a different user at that time which is not what I want.

Any tips on how to get a valid idToken from the currently logged in user for reauthentication?

Thanks!

like image 342
Riya Sinha Avatar asked Oct 19 '25 01:10

Riya Sinha


1 Answers

Just use reauthenticateWithPopup/reauthenticateWithRedirect.

firebase.auth().currentUser.reauthenticateWithPopup(new firebase.auth.GoogleAuthProvider())
  .then(function(userCredential) {
    // You can now delete the user:
    return firebase.auth().currentUser.delete();
  })
  .catch(function(error) {
    // Credential mismatch or some other error.
  });
like image 89
bojeil Avatar answered Oct 22 '25 03:10

bojeil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!