Skip to content

Commit f8abd2d

Browse files
committed
chore: fix lockfile
2 parents 187fa95 + df39573 commit f8abd2d

File tree

7 files changed

+62
-148
lines changed

7 files changed

+62
-148
lines changed

packages/velog-server/src/common/constants/TimeConstants.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ const ONE_MINUTE_IN_MS = 1000 * 60
22
const ONE_HOUR_IN_MS = ONE_MINUTE_IN_MS * 60
33
const ONE_DAY_IN_MS = ONE_HOUR_IN_MS * 24
44

5-
const ONE_MINUTE_S = 60
6-
const ONE_HOUR_S = ONE_MINUTE_S * 60
7-
const ONE_DAY_S = ONE_HOUR_S * 24
5+
const ONE_MINUTE_IN_S = 60
6+
const ONE_HOUR_IN_S = ONE_MINUTE_IN_S * 60
7+
const ONE_DAY_IN_S = ONE_HOUR_IN_S * 24
88

99
export const Time = {
1010
ONE_MINUTE_IN_MS,
1111
ONE_HOUR_IN_MS,
1212
ONE_DAY_IN_MS,
1313

14-
ONE_MINUTE_S,
15-
ONE_HOUR_S,
16-
ONE_DAY_S,
14+
ONE_MINUTE_IN_S,
15+
ONE_HOUR_IN_S,
16+
ONE_DAY_IN_S,
1717
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
1212
if (request.url.includes('/auth/logout')) return
1313

1414
const userService = container.resolve(UserService)
15+
const jwt = container.resolve(JwtService)
16+
1517
let accessToken: string | undefined = request.cookies['access_token']
1618
const refreshToken: string | undefined = request.cookies['refresh_token']
1719
const authorization = request.headers['authorization']
@@ -21,12 +23,11 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
2123
accessToken = authorization.split('Bearer ')[1]
2224
}
2325

24-
const jwt = container.resolve(JwtService)
25-
2626
if (!accessToken && !refreshToken) return
2727

2828
if (accessToken && refreshToken) {
2929
const accessTokenData = await jwt.decodeToken<AccessTokenData>(accessToken)
30+
3031
const diff = accessTokenData.exp * 1000 - new Date().getTime()
3132
// refresh token when life < 30mins
3233
if (diff < Time.ONE_MINUTE_IN_MS * 30 && refreshToken) {
@@ -46,9 +47,22 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
4647
} catch (e) {
4748
console.log('accessToken', accessToken)
4849
console.log('authPlugin error', e)
49-
const cookie = container.resolve(CookieService)
50-
cookie.clearCookie(reply, 'access_token')
51-
cookie.clearCookie(reply, 'refresh_token')
50+
51+
try {
52+
if (refreshToken) {
53+
const tokens = await userService.restoreToken({ request, reply })
54+
accessToken = tokens.accessToken
55+
56+
const accessTokenData = await jwt.decodeToken<AccessTokenData>(accessToken)
57+
request.user = { id: accessTokenData.user_id }
58+
} else {
59+
throw new Error()
60+
}
61+
} catch (error) {
62+
const cookie = container.resolve(CookieService)
63+
cookie.clearCookie(reply, 'access_token')
64+
cookie.clearCookie(reply, 'refresh_token')
65+
}
5266
}
5367
})
5468
}

packages/velog-server/src/lib/jwt/JwtService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class JwtService {
6363
},
6464
{
6565
subject: 'access_token',
66-
expiresIn: '24h',
66+
expiresIn: '1',
6767
},
6868
)
6969

packages/velog-server/src/routes/auth/v3/social/SocialController.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ export class SocialController implements Controller {
130130

131131
if (user) {
132132
const tokens = await this.jwt.generateUserToken(user.id)
133+
133134
this.cookie.setCookie(reply, 'access_token', tokens.accessToken, {
134-
maxAge: Time.ONE_HOUR_IN_MS * 24,
135+
maxAge: Time.ONE_DAY_IN_S,
135136
})
136137
this.cookie.setCookie(reply, 'refresh_token', tokens.refreshToken, {
137-
maxAge: Time.ONE_DAY_IN_MS * 30,
138+
maxAge: Time.ONE_DAY_IN_S * 30,
138139
})
139140

140141
const redirectUrl = ENV.clientV3Host
@@ -255,10 +256,10 @@ export class SocialController implements Controller {
255256

256257
const tokens = await this.jwt.generateUserToken(user.id)
257258
this.cookie.setCookie(reply, 'access_token', tokens.accessToken, {
258-
maxAge: Time.ONE_HOUR_IN_MS * 24,
259+
maxAge: Time.ONE_DAY_IN_S,
259260
})
260261
this.cookie.setCookie(reply, 'refresh_token', tokens.refreshToken, {
261-
maxAge: Time.ONE_DAY_IN_MS * 30,
262+
maxAge: Time.ONE_DAY_IN_S * 30,
262263
})
263264

264265
const profile = await this.db.userProfile.findFirst({

packages/velog-server/src/services/PostService/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ export class PostService implements Service {
463463
postIds = [...postIds, ...randomPostIds]
464464
}
465465

466-
this.redis.set(cacheKey, postIds.join(','), 'EX', Time.ONE_DAY_S)
466+
this.redis.set(cacheKey, postIds.join(','), 'EX', Time.ONE_DAY_IN_S)
467467
}
468468
const posts = await this.findPostsByIds(postIds)
469469
const normalized = this.utils.normalize(posts)

packages/velog-server/src/services/UserService/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ export class UserService implements Service {
144144
)
145145

146146
this.cookie.setCookie(ctx.reply, 'access_token', tokens.accessToken, {
147-
maxAge: Time.ONE_HOUR_IN_MS * 24,
147+
maxAge: Time.ONE_DAY_IN_S,
148148
})
149149
this.cookie.setCookie(ctx.reply, 'refresh_token', tokens.refreshToken, {
150-
maxAge: Time.ONE_DAY_IN_MS * 30,
150+
maxAge: Time.ONE_DAY_IN_S * 30,
151151
})
152152

153153
return tokens
@@ -254,7 +254,7 @@ export class UserService implements Service {
254254
throw error
255255
}
256256

257-
this.redis.set(key, data, 'EX', Time.ONE_MINUTE_S * 30)
257+
this.redis.set(key, data, 'EX', Time.ONE_MINUTE_IN_S * 30)
258258
}
259259
public async confirmChangeEmail(code: string, signedUserId?: string): Promise<void> {
260260
if (!signedUserId) {

0 commit comments

Comments
 (0)