(req: Request, res: Response, next: NextFunction)
| 12 | -----END PUBLIC KEY-----`; |
| 13 | |
| 14 | export function authMiddleware(req: Request, res: Response, next: NextFunction) { |
| 15 | const authHeader = req.headers.authorization; // Bearer token |
| 16 | const token = authHeader && authHeader.split(" ")[1]; |
| 17 | |
| 18 | if (!token) { |
| 19 | res.status(401).json({ message: "Unauthorized" }); |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | const decoded = jwt.verify(token, JWT_PUBLIC_KEY!, { |
| 24 | algorithms: ["RS256"], |
| 25 | }); |
| 26 | |
| 27 | if (!decoded) { |
| 28 | res.status(401).json({ message: "Unauthorized" }); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const userId = (decoded as any).sub; |
| 33 | |
| 34 | if (!userId) { |
| 35 | res.status(401).json({ message: "Unauthorized" }); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | req.userId = userId; |
| 40 | next(); |
| 41 | } |
nothing calls this directly
no outgoing calls
no test coverage detected