import { NextFunction , Request , Response } from 'express' ; import jwt from 'jsonwebtoken' ; import User from '../entities/User' ; export default async ( req , res , next ) => { try { const token = req . cookies . token ; if ( ! token ) return next ( ) ; const { username } = jwt . verify ( token , process . env . JWT_SECRET ! ) ; const user = await User . findOne ( { username } ) ; res . locals . user = user ; return next ( ) ; } catch ( err ) { console . log ( err ) ; return res . status ( 401 ) . json ( { error : 'Unauthenticated' } ) ; } } ;