122

The (unclear) example in the new docs:

var user = firebase.auth().currentUser;
var credential;
// Prompt the user to re-provide their sign-in credentials
user.reauthenticateWithCredential(credential).then(function() {

How should I create this credential object?

I tried:

  • reauthenticateWithCredential(email, password) (like the login method)
  • reauthenticateWithCredential({ email, password }) (the docs mention one argument only)

No luck :(

PS: I don't count the hours wasted searching for relevant info in the new docs... I miss so much the fabulous firebase.com docs, but wanted to switch to v3 or superior for firebase.storage...

7 Answers 7

243

I managed to make it work, docs should be updated to include this for who does not want to spend too much time in the exhaustive-but-hard-to-read API reference.

Firebase 8.x

The credential object is created like so:

const user = firebase.auth().currentUser;
const credential = firebase.auth.EmailAuthProvider.credential(
    user.email, 
    userProvidedPassword
);
// Now you can use that to reauthenticate
user.reauthenticateWithCredential(credential);

Firebase 9.x

(Thanks @Dako Junior for his answer that I'm adding here for exhaustivity)

import {
    EmailAuthProvider,
    getAuth,
    reauthenticateWithCredential,
} from 'firebase/auth'

const auth = getAuth()
const credential = EmailAuthProvider.credential(
    auth.currentUser.email,
    userProvidedPassword
)
const result = await reauthenticateWithCredential(
    auth.currentUser, 
    credential
)
// User successfully reauthenticated. New ID tokens should be valid.

Note

Some people asked about userProvidedPassword, if it was some sort of stored variable from the first login. It is not, you should open a new dialog/page with a password input, and the user will enter their password again.

I insist that you must not try to workaround it by storing user password in cleartext. This is a normal feature for an app. In GMail for example, sometimes your session expires, or there is a suspicion of hack, you change location, etc. GMail asks for your password again. This is reauthentication.

It won't happen often but an app using Firebase should support it or the user will be stuck at some point.

16
  • 5
    Great to hear that you found the solution! I'll add a note to update/clarify the docs. And keep in mind that there's also a feedback button on every page for that specific purpose. :-) Jun 14, 2016 at 13:44
  • 50
    3 years later and the docs still need to be updated! Thank you for saving me time. Feb 1, 2019 at 14:16
  • 24
    4 years later... yet the doc still "exhaustive-but-hard-to-read"
    – Nik
    Nov 23, 2019 at 2:49
  • 1
    @Oliver, this codes relates to when the user needs to reauthenticate (hence the name of the method). In that event, like with the sign-in form, they need to provide their password again. Never store user passwords yourself. (or, well, do it properly but in that case you aren't using Firebase auth :) )
    – Pandaiolo
    Mar 3, 2020 at 16:19
  • 1
    @FotiosTsakiris you should definitely not trying to workaround it by storing user password in cleartext, this seems like a normal feature for an app. In GMail for example, sometimes your session expires, or there is a suspicion of hack, you change location, etc. GMail asks for your password again. This is reauthentication. It won't happen often but an app using Firebase should support it or the user will be stuck at some point. Adding this to the answer for clarity.
    – Pandaiolo
    Jan 5, 2022 at 9:52
40

Complete answer - you can use the following:

var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credentials);

Please note that reauthenticateWithCredential is the updated version of reauthenticate()

5
  • 4
    Thanks for adding the third line that is missing from the accepted answer May 27, 2017 at 21:20
  • 1
    @Oliver the password should be the one that already exists with that email.
    – maudulus
    Mar 2, 2020 at 17:22
  • Not sure that it was @Sandokan, seeing here: firebase.google.com/docs/reference/js/… - where did you see it deprecated?
    – maudulus
    Nov 27, 2020 at 16:33
  • @Sandokan I believe you are mistaken - reauthenticateAndRetrieveDataWithCredential is deprecated: firebase.google.com/docs/reference/js/…
    – maudulus
    Nov 28, 2020 at 15:45
  • @maudulus. Yes. you are right. In my case, it was firebase version issue. I was using firebase5.7. firebase.User.prototype.reauthenticateWithCredential is deprecated. Please use firebase.User.prototype.reauthenticateAndRetrieveDataWithCredential instead.
    – Sandokan
    Nov 30, 2020 at 9:48
6

With the new firebase version 9.*

import {
  EmailAuthProvider,
  getAuth,
  reauthenticateWithCredential,
} from "firebase/auth";

const auth = getAuth();


let credential = EmailAuthProvider.credential(
                  auth.currentUser.email,
                  password
                );

reauthenticateWithCredential(auth.currentUser, credential)
.then(result => {
      // User successfully reauthenticated. New ID tokens should be valid.
    })
5

There are multiple methods to re-authenticat. See the refs: https://firebase.google.com/docs/reference/js/firebase.User

firebase
.auth()
.currentUser.reauthenticateWithPopup(new firebase.auth.GoogleAuthProvider())
.then((UserCredential) => {
    console.log("re-outh", UserCredential);
});

In case your app allows multiple authentication methods you might want to first find out what privider was used. You can do this by looking at the firebase.auth().currentUser.providerData array.

0

I agree that the documentation is not pretty clear on this. But looking a little deeper on the API reference I found firebase.auth.AuthCredential and this and I guess you should be looking to pass it to reauthenticate().

I'm guessing here but I would start trying to log the firebase.auth() to see if there is any credential object there.

I suppose it will look something like the following:

user.reauthenticate(firebase.auth().credential).then(function() {
1
  • I managed to get it to work, I'm writing an answer. You forgot the email and password in your answer :)
    – Pandaiolo
    Jun 14, 2016 at 12:46
0

Now there's a small change in the method since both posted answers are deprecated,

    val user = auth.currentUser
    user?.let { _user ->
        val credentials = EmailAuthProvider.getCredential(
            _user.email!!,
            "userPassword"
        )
        _user.reauthenticate(credentials).addOnCompleteListener { _reauthenticateTask ->
  }
-2
final FirebaseUser fireBaseUser = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider.getCredential(fireBaseUser.getEmail(), storedPassword);
fireBaseUser.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
     @Override
     public void onComplete(@NonNull Task<Void> reAuthenticateTask) {
          if (!reAuthenticateTask.isSuccessful())
               ...
     }
});
0

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.