如何创建 Firebase web user.reenticateWithCredential()方法所需的“凭证”对象?

新文件中的例子(不清楚) :

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

我应该如何创建这个 credential对象?

我试过:

  • reauthenticateWithCredential(email, password)(类似于登录方法)
  • (文件只提到一个论点)

运气不好: (

PS: 我没有计算在新文档中搜索相关信息所浪费的时间... ... 我非常怀念那些精彩的 firebase.com 文档,但是我想转到 firebase.Storage 的 v3或者 super... ..。

36158 次浏览

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() {

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.

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())
...
}
});

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()

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 ->
}

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.

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.
})