Skip to content

Commit d49516e

Browse files
committed
update: authentication logic and add user existence check
1 parent 3eb23b7 commit d49516e

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
3535
await userService.restoreToken({ request, reply })
3636
}
3737

38-
// const user = await userService.findById(accessTokenData.user_id)
39-
40-
// if (!user) {
41-
// cookie.clearCookie(reply, 'access_token')
42-
// cookie.clearCookie(reply, 'refresh_token')
43-
// throw new Error('User not found')
44-
// }
38+
const user = await userService.checkExistsUser(accessTokenData.user_id)
39+
if (!user) {
40+
cookie.clearCookie(reply, 'access_token')
41+
cookie.clearCookie(reply, 'refresh_token')
42+
throw new Error('User not found')
43+
}
4544

4645
request.user = { id: accessTokenData.user_id }
4746
return

Diff for: packages/velog-server/src/services/UserService/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ export class UserService implements Service {
319319
}
320320
public async checkExistsUser(userId?: string): Promise<boolean> {
321321
if (!userId) return false
322+
322323
const key = this.redis.generateKey.existsUser(userId)
323324
const value = await this.redis.get(key)
324325
if (value === 'true') return true
325326
if (value === 'false') return false
327+
326328
const user = await this.findById(userId)
327329
const save = user ? 'true' : 'false'
328330
await this.redis.set(key, save, 'EX', Time.ONE_MINUTE_IN_S * 10)

0 commit comments

Comments
 (0)