Skip to content

Commit 99aa197

Browse files
committed
fix(auth): verify JWT access tokens
1 parent 8628a4b commit 99aa197

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

middleware/auth.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
import jwt from "jsonwebtoken";
12
import { storeToRefs } from "pinia";
23

34
export default defineNuxtRouteMiddleware((to) => {
5+
const runtimeConfig = useRuntimeConfig();
46
const { user } = storeToRefs(useAuthStore());
57

68
console.log(user.value);
79

810
if (to.fullPath === "/login" && user.value.id) return navigateTo("/");
11+
12+
if (!user.value.id) return navigateTo("/login");
13+
else
14+
try {
15+
// Verify JWT access token
16+
const verificationPayload = jwt.verify(
17+
user.value.accessToken,
18+
runtimeConfig.jwtAccessSecret,
19+
);
20+
21+
if ((verificationPayload as jwt.JwtPayload).jti)
22+
return navigateTo(to.fullPath);
23+
else return navigateTo("/login");
24+
} catch (err) {
25+
throw createError({
26+
statusCode: 500,
27+
statusMessage: `Server error: ${(err as Error).name}`,
28+
});
29+
}
930
});

0 commit comments

Comments
 (0)