Currently, I am developing a server using Express and Typescript. I have integrated passport js for authenticating the routes I have set up. However, one issue that I encounter is that Express.Request.user
is defined as Express.User | undefined
. This means that in every protected handler, I need to verify if the request is truthy just to satisfy Typescript.
app.get(
'/authenticated',
authPassport.authenticate('jwt', { session: false }),
(req, res) => {
if(!req.user) throw new Error();
console.log(req.user.email)
res.send('you are authenticated')
}
)
I am wondering if there is a solution to modify the type of req.user
to ensure it always has a definite value User
?