I am trying to implement an express API that authenticates via a jwt token and I'm using the express-jwt middleware to authenticate access to a method:
import express from 'express'
import jwt from 'express-jwt'
const app = express()
app.get('/protected', jwt({ secret: 'jwtSecret', algorithms: ['HS256'] }), prot)
const prot = (
req: express.Request,
res: express.Response,
_next: express.NextFunction
): void => {
res.json({
msg: req.user,
})
}
app.listen(3000)
But I get this error:
Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.ts(2339)
I've tried attaching req: express.Request & { user: unknown }
, but then the function doesn't fit with express anymore.
Any ideas what I can do here. If worse comes to worse, can I just tell TypeScript to shut up somehow, since I know that the field will be there (even though I know that's not the point of TypeScript)