使用 Firebase 重新进行身份验证 [英] Using Firebase reauthenticate

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

问题描述

我将非常感谢有关如何在 Firebase 中重新验证用户身份的帮助.如果文档没有解释如何使用它,我想知道添加所有这些强大的功能是否有意义:

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

在构造函数中:

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

然后是函数

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

推荐答案

reauthenticate() 方法在 firebase.User,不在 firebase.auth.Auth 本身.

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

<小时>

更新(2017 年 7 月):

Firebase Web SDK 4.0 版有一些重大变化.来自发行说明:

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

突破:firebase.User.prototype.reauthenticate 已被移除,取而代之的是 firebase.User.prototype.reauthenticateWithCredential.

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

据我所知,reauthenticateWithCredential 是旧方法的替代品.

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

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

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