首页 > 解决方案 > Firebase reauthenticateAndRetrieveDataWithCredential 问题

问题描述

方法reauthenticateAndRetrieveDataWithCredential需要credential.

我试过这个,它给了我一个错误:

const user = firebase.auth().currentUser;
const credential = firebase.auth.OAuthCredential;
await user.reauthenticateAndRetrieveDataWithCredential(credential);
await user.updateEmail(email);
return email;

错误信息

reauthenticateAndRetrieveDataWithCredential 失败:第一个参数“凭据”必须是有效的凭据。

我只有宣誓认证(没有电子邮件+密码)。所以我无法弄清楚credential火力基地需要什么。有什么帮助吗?

编辑: 出于某种原因,我的firebase.auth.OAuthCredential或 ( firebase.auth.AuthCredential) 返回未定义。用户已登录/验证。

标签: javascriptfirebasefirebase-authentication

解决方案


在一个 Vue.js 项目中遇到了这个问题,我们有一个firebase.js文件来处理const.

希望这将有助于某人在使用类似设置时节省时间。

文件:firebase.js

import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'


// Initialize Firebase
const app = firebase.initializeApp(options)
export const fb = firebase
export const auth = app.auth()
export const db = app.database()
export const functions = app.functions()

changePassword()在其他脚本文件或里面的例子*.vue

import { fb, auth } from './firebase.js'
...
changePassword() {
  if (yourFormValidation == true) {
    let user = auth.currentUser

    const credentials = fb.auth.EmailAuthProvider.credential(
      user.email,
      this.current_password
    )
    user.reauthenticateAndRetrieveDataWithCredential(credentials)
      .then(() => {
        user.updatePassword(this.new_password)
          .then(() => {
            console.log('your password was successfully changed.')
          })
          .catch(error => console.log(error))
      })
      .catch(error => console.log(error.message))
  }
}

我遇到的地方Cannot read property 'credential' of undefined"...

仅导入{ auth } from './firebase.js然后调用auth.EmailAuthProvider().

如何访问在firebase.auth()上找到的其他类...

  1. export const fb = firebasefirebase.js

  2. import { fb, auth } from './firebase.js'你在哪里写你的函数

  3. 需要电话fb.auth.EmailAuthProvider.credential()或其他课程


推荐阅读