I’m working on a Node.js backend using Firebase Admin SDK and TypeScript. After installing the Firebase Admin package (firebase-admin: ^12.1.1), I started encountering the following TypeScript errors and shows type errors of all api:
TSError: ⨯ Unable to compile TypeScript:
src/middleware/auth.ts:21:26 - error TS2339: Property 'id' does not exist on type 'string | JwtPayload'.
Property 'id' does not exist on type 'string'.
21 uid: decoded.id,
~~
src/middleware/auth.ts:22:27 - error TS2339: Property 'role' does not exist on type 'string | JwtPayload'.
Property 'role' does not exist on type 'string'.
...
Problem:
I’m using firebase-admin to verify JWT tokens in my middleware. The decoded token (decodedToken) is expected to have properties like id and role, but TypeScript is giving errors indicating that these properties do not exist on the type string | JwtPayload.
Here’s my middleware code:
ts
Copy code
import { Request, Response, NextFunction } from 'express';
import admin from 'firebase-admin';
export const authenticate = async (req: Request, res: Response, next: NextFunction) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).send({ error: 'No token provided' });
}
try {
const decoded = await admin.auth().verifyIdToken(token);
// Error: Property 'id' does not exist on type 'string | JwtPayload'
req.user = {
uid: decoded.id,
role: decoded.role
};
next();
} catch (error) {
return res.status(403).send({ error: 'Unauthorized' });
}
The last overload gave the following error.
Argument of type 'NextHandleFunction' is not assignable to parameter of type 'PathParams'.
43 app.use(bodyParser.json({ limit: "50mb" }));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/express-serve-static-core/express-serve-static-core.d.ts:38:9
38 (path: PathParams, ...handlers: RequestHandlerParams[]): T;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The last overload is declared here.
at createTSError (/Users/ankushtagore/projects/rentara/simplerStayBackend-v1/node_modules/ts-node/src/index.ts:859:12)
};
My package.json
{
"name": "rentara-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node dist/app.js",
"build": "tsc -p .",
"dev": "nodemon src/app.ts"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^18.14.2",
"nodemon": "^2.0.22",
"sequelize-cli": "^6.6.2",
"ts-node": "^10.9.1",
"tslint": "^6.1.3",
"typescript": "^4.9.5"
},
"dependencies": {
"@sendgrid/mail": "^7.7.0",
"@supabase/supabase-js": "^2.39.8",
"aws-sdk": "^2.1325.0",
"bcrypt": "^5.0.0",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"cron": "^2.2.0",
"dotenv": "^8.6.0",
"express": "^4.18.2",
"express-serve-static-core": "^0.1.1",
"firebase-admin": "^12.4.0",
"path": "^0.12.7",
"pg": "^8.5.1",
"pm2": "^5.3.0",
"sequelize": "^6.32.0",
}
}
Checked that firebase-admin is properly installed and configured.
Added skipLibCheck to tsconfig.json to ignore type checking in node_modules, but the error persists.
Tried narrowing the type, but Firebase Admin’s token verification doesn’t return the expected id and role properties.
Used inbuilt express parser.
It works when i remove the firebase-admin package