Typescript ExpressJS middleware for jwt auth

hey guys! I am new to ts. I am trying to create a expressJS middleware for verifying jwt token. And after successful verification i want to attach the user object i get from payload which is typed string | jwt.JwtPayload in request object. but typescript is complaining. I dont want to just make it work i want to do it the correct way. help me out or Guide me to relavant resources.
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { env } from '../types/env';

export const verifyToken = (req: Request, res: Response, next: NextFunction) => {
  try {
    const token = req.header('authorization')?.replace('Bearer ', '');

    if (!token) {
      return res.status(401).json({ message: 'Missing access token' });
    }

    const user = jwt.verify(token, env.ACCESS_TOKEN_SECRET);

    // req.user = user;
    next();
  } catch (error) {
    console.log(error);
    res.status(403).json({ message: 'Invalid access token' });
  }
};
Was this page helpful?