生成 Firebase 重新身份验证所需的凭据对象



当尝试构建凭据对象以使用 Firebase 和 ReactJS 重新验证用户时,我得到无法读取未定义的属性"凭据">,其中undefined指的是应该firebase.auth.EmailAuthProviderthis.app.auth.EmailAuthProvider

我已经读到它是一种静态方法,不能在实例上调用,但我不确定这到底是什么意思,或者如何纠正我的实现以获得所需的凭据。我正在基于类的组件中调用该方法,但我仍然不知道所有这些与调用静态方法有何关系。

我调用的方法是'this.app.auth.EmailAuthProvider.credential(('

reAuthUser = (data) => {
// console.log('email: ', data.userEmail, 'password: ', data.password);
const userCredential = this.app.auth.EmailAuthProvider.credential(
data.email,
data.password
)
// this.currentUser.reauthenticateWithCredential(data.userEmail, data.password)
//     .then(function() {
//         alert('user has been reauthenticated')
//     }).catch(function(error) {
//         console.log('reauth error: ', error)
//     })
};

这是在ReactJS中,在一个类组件中。this.app是对 Firebase 的引用,在构造函数中调用:

constructor(props) {
super(props);
this.app = Firebase;
this.currentUser = this.app.auth().currentUser;
};

我知道类似的问题已经被问过,答案也得到了批准,但在这一点上它们对我来说没有多大意义。

假设this.app.authfirebase.auth.Auth类的实例,EmailAuthProvider不会出现在该对象上,因为它不是firebase.auth.Auth类原型的一部分。

相反,EmailAuthProviderfirebase.auth命名空间的一部分,这意味着它只能使用firebase.auth.EmailAuthProvider访问(请注意auth如何不作为函数调用(。

如果使用导入,您还可以使用

import { auth as FirebaseAuth } from 'firebase';
FirebaseAuth.EmailAuthProvider.credential(...)

最新更新