使用Firebase重新验证 [英] Using Firebase reauthenticate

查看:212
本文介绍了使用Firebase重新验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您提供有关如何在Firebase中重新验证用户身份的帮助。我想知道如果说文档没有解释如何使用它,那么是否有必要添加所有这些优秀的功能:



目前,这是我正在尝试的,它不工作。错误无法读取未定义的属性'凭证'



在构造函数中:

 构造函数(@Inject(FirebaseApp)firebaseApp:any){
this.auth = firebaseApp.auth();
console.log(this.auth);





然后函数

  changePassword(passwordData){
if(passwordData.valid){
console.log(passwordData.value);
//让我们先重新验证,不管
//用户已经登录了多久!

const user = this.auth.currentUser;
const credential = this.auth.EmailAuthProvider.credential(user.email,passwordData.value.oldpassword);
console.log(凭证);
this.auth.reauthenticate(credential)
.then((_)=> {
console.log('User reauthenticated');
this.auth.updatePassword( ((_)=> {
console.log('Password changed');
))
.catch((error) =($ error)= $ b $ console.log );
})
}
}


解决方案 reauthenticate()方法在 firebase.User ,不在
  var user = firebase.app.auth() 。当前用户; 
var credentials = firebase.auth.EmailAuthProvider.credential('puf@firebaseui.com','firebase');
user.reauthenticate(credentials);






更新(2017年7月) strong>:

Firebase Web SDK 4.0版本中有一些重大变化。从
发行说明


BREAKING: firebase.User.prototype.reauthenticate 已被删除,转而使用 firebase.User.prototype.reauthenticateWithCredential

据我所知, reauthenticateWithCredential 是一个下拉菜单,替换旧的方法。

I'll appreciate assistance with how to reauthenticate a user in Firebase. I wonder if it makes any sense adding all these great features if the documentation doesn't explain how to use it:

Currently, this is what I'm trying, and it ain't working. Errors as cannot read property 'credential' of undefined

In constructor:

  constructor(@Inject(FirebaseApp) firebaseApp: any) {
    this.auth = firebaseApp.auth();
    console.log(this.auth);
  }

then the function

changePassword(passwordData) {
    if(passwordData.valid) {
      console.log(passwordData.value);
      // let us reauthenticate first irrespective of how long
      // user's been logged in!

      const user = this.auth.currentUser;
      const credential = this.auth.EmailAuthProvider.credential(user.email, passwordData.value.oldpassword);
      console.log(credential);
      this.auth.reauthenticate(credential)
        .then((_) => {
          console.log('User reauthenticated');
          this.auth.updatePassword(passwordData.value.newpassword)
            .then((_) => {
              console.log('Password changed');
            })
            .catch((error) => {
              console.log(error);
            })
        })
        .catch((error) => {
          console.log(error);
        })
    }
  }

解决方案

The reauthenticate() method is called on a firebase.User, not on firebase.auth.Auth itself.

var user = firebase.app.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential('puf@firebaseui.com', 'firebase');
user.reauthenticate(credentials);


Update (July 2017):

There are some breaking change in the 4.0 version of the Firebase Web SDK. From the release notes:

BREAKING: firebase.User.prototype.reauthenticate has been removed in favor of firebase.User.prototype.reauthenticateWithCredential.

As far as I can tell the reauthenticateWithCredentialis a drop-in replacement for the old method.

这篇关于使用Firebase重新验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆