首页 > 解决方案 > 如何获取有效的 Firebase 用户 JWT 令牌

问题描述

鉴于我有一个 Firebase 项目,但没有支持 Firebase SDK 的开发环境,我如何才能为 Firebase 项目中的一位用户获取有效的 Firebase JWT?我能够在项目中创建云函数,并尝试为此创建一个函数(在 TypeScript 中),但无济于事:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

export const login = functions.https.onRequest((req, res) => {
  const email: string = req.query.email || req.body.email;
  const password: string = req.query.password || req.body.password;
  admin.auth().signInWithEmailAndPassword(email, password);
  admin.auth().currentUser.getIdToken(true).then((idToken: any) => {
    functions.logger.info("token: " + idToken, {structuredData: true});
    res.status(200).send(idToken);
  }).catch((error: any) => {
    functions.logger
        .info("could not get token: " + error, {structuredData: true});
   res.status(500).send(error);
  });
});

由于以下错误,此代码不会部署:

Property 'signInWithEmailAndPassword' does not exist on type 'Auth'.
Property 'currentUser' does not exist on type 'Auth'.

标签: firebaseauthenticationgoogle-cloud-functions

解决方案


推荐阅读