|
| 1 | +import { Role } from "@prisma/client"; |
| 2 | +import { type H3Event } from "h3"; |
| 3 | +import jwt from "jsonwebtoken"; |
| 4 | + |
| 5 | +const protectRoute = async (event: H3Event, role: Role = Role.USER) => { |
| 6 | + const runtimeConfig = useRuntimeConfig(); |
| 7 | + const authHeaders = event.headers.get("Authorization"); |
| 8 | + |
| 9 | + if (authHeaders === null || authHeaders === "") |
| 10 | + throw createError({ |
| 11 | + statusCode: 401, |
| 12 | + statusMessage: "Unauthorized: No headers passed.", |
| 13 | + }); |
| 14 | + |
| 15 | + // Authorization: Bearer [jwt....] |
| 16 | + const [, bearerToken] = authHeaders.split(" "); |
| 17 | + const bearerVerified = jwt.verify( |
| 18 | + bearerToken, |
| 19 | + runtimeConfig.jwtAccessSecret, |
| 20 | + ); |
| 21 | + |
| 22 | + if (new Date().getTime() > (bearerVerified as jwt.JwtPayload).exp! * 1000) |
| 23 | + throw createError({ |
| 24 | + statusCode: 401, |
| 25 | + statusMessage: "Unauthorized: invalid token", |
| 26 | + }); |
| 27 | + |
| 28 | + if (!(bearerVerified as jwt.JwtPayload).userId) |
| 29 | + throw createError({ |
| 30 | + statusCode: 401, |
| 31 | + statusMessage: "Unauthorized: invalid token", |
| 32 | + }); |
| 33 | + |
| 34 | + // check role verification |
| 35 | + const user = await db.user.findFirst({ |
| 36 | + where: { |
| 37 | + id: (bearerVerified as jwt.JwtPayload).userId, |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + if (!user) |
| 42 | + throw createError({ |
| 43 | + statusCode: 401, |
| 44 | + statusMessage: "Unauthorized: invalid user", |
| 45 | + }); |
| 46 | + |
| 47 | + if (user.role !== role && user.role !== Role.ADMIN) |
| 48 | + throw createError({ |
| 49 | + statusCode: 401, |
| 50 | + statusMessage: "Unauthorized: invalid permissions", |
| 51 | + }); |
| 52 | + |
| 53 | + return { user }; |
| 54 | +}; |
| 55 | + |
| 56 | +export default protectRoute; |
0 commit comments