File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ import jwt from "jsonwebtoken" ;
1
2
import { storeToRefs } from "pinia" ;
2
3
3
4
export default defineNuxtRouteMiddleware ( ( to ) => {
5
+ const runtimeConfig = useRuntimeConfig ( ) ;
4
6
const { user } = storeToRefs ( useAuthStore ( ) ) ;
5
7
6
8
console . log ( user . value ) ;
7
9
8
10
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
+ }
9
30
} ) ;
You can’t perform that action at this time.
0 commit comments