File size: 664 Bytes
178b3d8
920bbce
c57d845
178b3d8
 
920bbce
 
 
 
 
 
178b3d8
c57d845
920bbce
 
 
 
 
 
 
178b3d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Response, NextFunction } from 'express';
import { verifyToken } from '../utils/jwtUtils';
import { AuthenticatedRequest, UserInterface } from '../shared/interfaces/user.interface';

const jwtMiddleware = (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
    const token = req.header('authorization')?.split(' ')[1];
    if (!token) {
        return res.status(401).json({ error: 'Unauthorized' });
    }

    try {
        const user = verifyToken(token);
        req.user = user as UserInterface;
        next();
    } catch (error) {
        return res.status(401).json({ error: 'Unauthorized' });
    }
    
}

export {jwtMiddleware}