How to reauthorize a user in Firebase?

Viewed 949

Environment

  • Firebase JavaScript SDK v8

Question

How can I re-authorize an already-logged-in user with their password? What I want to do is like below:

const password = "some-password"
firebase.reAuthorizeUser(password)
  .then(() => console.log("ok"))
  .catch(() => console.log("ng"))

Thank you in advance.

3 Answers

You are looking for reauthenticateWithCredential method.

const user = firebase.auth().currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

user.reauthenticateWithCredential(credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error ocurred
  // ...
});

Checkout reauthenticate a user in the documentation.

SDK v9

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

  const reauthenticate = async () => {
    try {
        const auth = getAuth();
        const user = auth.currentUser;
        const credential = await EmailAuthProvider.credential(
            email,
            password
        );
        await reauthenticateWithCredential(user, credential);
        return true
    } catch (e) {
        return null
    }
}

const credential = promptForCredentials();

this is giving me error

cant find variable: promptForCredentials.

Related