如何从 Firebase Admin Node.js SDK 导入类型?

How to import types from Firebase Admin Node.js SDK?

我正在尝试导入 DecodedIDToken 类型 (https://firebase.google.com/docs/reference/admin/node/firebase-admin.auth.decodedidtoken),以便我可以在验证 ID 令牌时将类型分配给 .then() 回调中的值通过 admin.auth().verifyIdToken().

我无法从 firebase-admin 导入 DecodedIDToken 类型,我不确定问题到底是什么。是不是因为firebase-admin模块只导出了admin

import { Request, Response } from "express";
import admin from 'firebase-admin';

admin.initializeApp();

const isAuthenticated = (req: Request, res: Response) => {
    const { authorization } = req.headers;

    if (authorization == null || !authorization.startsWith("Bearer")) {
        return res.status(401).json({ message: "Unauthorized" });
    }

    const split = authorization.split("Bearer ");
    if (split.length !== 2) {
        return res.status(401).json({ message: "Unauthorized" });
    }

    const token = split[1];

    try {
        admin.auth().verifyIdToken(token)
        .then((decodedIdToken: DecodedIDToken) => {
            req.userIdToken = decodedIdToken;
        })
        .catch();
    } catch (error) {
        console.log("error: " + error);
    }
}

export default isAuthenticated;

DecodedIdToken 从 Firebase Admin Auth SDK 导出。

import { DecodedIdToken } from "firebase-admin/auth";

试试吧:

import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier';