【问题标题】:FireBase + React: TypeError: user.getToken is not a functionFireBase + React:TypeError:user.getToken 不是函数
【发布时间】:2020-10-12 23:18:58
【问题描述】:

这里的代码 signInWithEmailAndPassword 可以完美运行,但是当我尝试获取令牌时,总是会抛出错误 TypeError: user.getToken is not a function

import app from "./base.js";

app.auth()
   .signInWithEmailAndPassword(email, password)
   .then(function (user) {
      user.getToken().then(function (token) {
        localForage.setItem("token", token); // store the token
        console.log("pusing forward");
        history.push("/");
      });
    }).catch((error) => {
        console.log("err: " + error);
     });

base.js

import firebase from "firebase/app";
import "firebase/auth";

const app = firebase.initializeApp({
   apiKey: process.env.REACT_APP_FIREBASE_KEY,
   authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
   databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
   projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
   storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
   messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID,
});

export default app;

欢迎评论谢谢。

【问题讨论】:

    标签: javascript reactjs firebase firebase-authentication jwt


    【解决方案1】:

    根据signInWithEmailAndPassword() 的API 文档,user 是一个UserCredential 对象。它没有getToken 方法。也许您想使用它的user 属性来获取User 对象,该对象具有getIdToken() 方法。

        user.user.getIdToken().then(token => { ... })
    

    这是调用signInWithEmailAndPassword(email, password) 后返回的对象user1 的峰值

    【讨论】:

      猜你喜欢
      • 2020-07-01
      • 2020-03-14
      • 2019-06-07
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2017-10-07
      相关资源
      最近更新 更多