简体   繁体   中英

How to reauthorize a user in Firebase?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM