@@ -5,6 +5,7 @@ import { container } from 'tsyringe'
5
5
import { UserService } from '@services/UserService/index.js'
6
6
import { CookieService } from '@lib/cookie/CookieService.js'
7
7
import { Time } from '@constants/TimeConstants.js'
8
+ import { DbService } from '@lib/db/DbService'
8
9
9
10
const authPlugin : FastifyPluginAsync = async ( fastify ) => {
10
11
fastify . decorateRequest ( 'user' , null )
@@ -13,6 +14,7 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
13
14
14
15
const userService = container . resolve ( UserService )
15
16
const jwt = container . resolve ( JwtService )
17
+ const cookie = container . resolve ( CookieService )
16
18
17
19
let accessToken : string | undefined = request . cookies [ 'access_token' ]
18
20
const refreshToken : string | undefined = request . cookies [ 'refresh_token' ]
@@ -34,6 +36,14 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
34
36
await userService . restoreToken ( { request, reply } )
35
37
}
36
38
39
+ const user = await userService . findById ( accessTokenData . user_id )
40
+
41
+ if ( ! user ) {
42
+ cookie . clearCookie ( reply , 'access_token' )
43
+ cookie . clearCookie ( reply , 'refresh_token' )
44
+ throw new Error ( 'User not found' )
45
+ }
46
+
37
47
request . user = { id : accessTokenData . user_id }
38
48
return
39
49
}
@@ -62,7 +72,6 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
62
72
}
63
73
} catch ( error ) {
64
74
console . log ( 'refresh token error' , error )
65
- const cookie = container . resolve ( CookieService )
66
75
cookie . clearCookie ( reply , 'access_token' )
67
76
cookie . clearCookie ( reply , 'refresh_token' )
68
77
}
0 commit comments