Skip to content

Commit 5dd6034

Browse files
committed
add: DbService import and handle user not found error in authPlugin
1 parent eba2482 commit 5dd6034

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/velog-server/src/common/plugins/global/authPlugin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { container } from 'tsyringe'
55
import { UserService } from '@services/UserService/index.js'
66
import { CookieService } from '@lib/cookie/CookieService.js'
77
import { Time } from '@constants/TimeConstants.js'
8+
import { DbService } from '@lib/db/DbService'
89

910
const authPlugin: FastifyPluginAsync = async (fastify) => {
1011
fastify.decorateRequest('user', null)
@@ -13,6 +14,7 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
1314

1415
const userService = container.resolve(UserService)
1516
const jwt = container.resolve(JwtService)
17+
const cookie = container.resolve(CookieService)
1618

1719
let accessToken: string | undefined = request.cookies['access_token']
1820
const refreshToken: string | undefined = request.cookies['refresh_token']
@@ -34,6 +36,14 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
3436
await userService.restoreToken({ request, reply })
3537
}
3638

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+
3747
request.user = { id: accessTokenData.user_id }
3848
return
3949
}
@@ -62,7 +72,6 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
6272
}
6373
} catch (error) {
6474
console.log('refresh token error', error)
65-
const cookie = container.resolve(CookieService)
6675
cookie.clearCookie(reply, 'access_token')
6776
cookie.clearCookie(reply, 'refresh_token')
6877
}

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)