Spaces:
Runtime error
Runtime error
Commit ·
920bbce
1
Parent(s): 737e718
add authMiddleware
Browse files
src/middlewares/authMiddleware.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Request, Response, NextFunction } from 'express';
|
| 2 |
+
import { verifyToken } from '../utils/jwtUtils';
|
| 3 |
+
const jwtMiddleware = (req: Request, res: Response, next: NextFunction) => {
|
| 4 |
+
const token = req.header('authorization')?.split(' ')[1];
|
| 5 |
+
if (!token) {
|
| 6 |
+
return res.status(401).json({ error: 'Unauthorized' });
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
try {
|
| 10 |
+
const decoded = verifyToken(token);
|
| 11 |
+
next();
|
| 12 |
+
} catch (error) {
|
| 13 |
+
return res.status(401).json({ error: 'Unauthorized' });
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
export {jwtMiddleware}
|