Spaces:
Sleeping
Sleeping
Download src/middleware/auth.ts from eneeyan/iqmath-backend-api: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/eneeyan/iqmath-backend-api/resolve/main/src/middleware/auth.ts
- Command line
-
hf download hf://spaces/eneeyan/iqmath-backend-api/src/middleware/auth.ts
-
curl -L -o auth.ts https://huggingface.co/spaces/eneeyan/iqmath-backend-api/resolve/main/src/middleware/auth.ts
1.22 kB
| import { NextFunction, Request, Response } from "express"; | |
| import jwt from "jsonwebtoken"; | |
| import { UserRole } from "@prisma/client"; | |
| import { JwtPayload } from "../types/auth"; | |
| export type AuthRequest = Request & { | |
| user?: JwtPayload; | |
| }; | |
| export function requireAuth(req: AuthRequest, res: Response, next: NextFunction) { | |
| const authHeader = req.headers.authorization; | |
| if (!authHeader?.startsWith("Bearer ")) { | |
| return res.status(401).json({ message: "Missing Bearer token" }); | |
| } | |
| const token = authHeader.replace("Bearer ", ""); | |
| const secret = process.env.JWT_SECRET; | |
| if (!secret) return res.status(500).json({ message: "JWT_SECRET not configured" }); | |
| try { | |
| const decoded = jwt.verify(token, secret) as JwtPayload; | |
| req.user = decoded; | |
| return next(); | |
| } catch { | |
| return res.status(401).json({ message: "Invalid token" }); | |
| } | |
| } | |
| export function requireRole(roles: UserRole[]) { | |
| return (req: AuthRequest, res: Response, next: NextFunction) => { | |
| if (!req.user) return res.status(401).json({ message: "Unauthorized" }); | |
| if (!roles.includes(req.user.role)) return res.status(403).json({ message: "Forbidden" }); | |
| return next(); | |
| }; | |
| } | |