Skip to content

Commit f20215d

Browse files
committed
feat(backend): API route protection, profile API route
1 parent d39d774 commit f20215d

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

server/api/user/profile.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import protectRoute from "~/server/utils/protectRoute";
2+
3+
export default defineEventHandler(async (event) => {
4+
const route = await protectRoute(event);
5+
6+
return {
7+
id: route.user.id,
8+
username: route.user.username,
9+
email: route.user.email,
10+
role: route.user.role,
11+
createdAt: route.user.createdAt,
12+
};
13+
});

server/utils/protectRoute.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)