Jonathan Wang
Jonathan Wang

Reputation: 334

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

I'm trying to import the DecodedIDToken type (https://firebase.google.com/docs/reference/admin/node/firebase-admin.auth.decodedidtoken) so that I can assign a type to the value in the .then() callback for when I verify an ID token via admin.auth().verifyIdToken().

I'm unable to import the DecodedIDToken type from firebase-admin and I'm not sure what exactly the issue is. Is it because only admin is exported from the firebase-admin module?

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;

Upvotes: 5

Views: 1907

Answers (2)

Bruno Assis Carvalho
Bruno Assis Carvalho

Reputation: 31

Just try it:

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

Upvotes: 0

Dharmaraj
Dharmaraj

Reputation: 50830

The DecodedIdToken is exported from Firebase Admin Auth SDK.

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

Upvotes: 6

Related Questions