Skip to content

Commit 29bff99

Browse files
committed
fix: authorization error
1 parent 960439d commit 29bff99

File tree

9 files changed

+119
-41
lines changed

9 files changed

+119
-41
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
.env.*
88
!**/**/.env.example
99

10-
.vscode
10+
.vscode

infrastructure/Pulumi.production.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
config:
22
aws:region: ap-northeast-2
33
velog:DOCKER_ENV: production
4-
velog:target: server
4+
velog:target: server,web

infrastructure/Pulumi.stage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
config:
22
aws:region: ap-northeast-2
33
velog:DOCKER_ENV: stage
4-
velog:target: server
4+
velog:target: web

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,35 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
1919
const authorization = request.headers['authorization']
2020

2121
try {
22-
if (!accessToken && authorization) {
22+
if (!accessToken && !!authorization && typeof authorization === 'string') {
2323
accessToken = authorization.split('Bearer ')[1]
2424
}
2525

2626
if (!accessToken && !refreshToken) return
2727

28-
if (accessToken && refreshToken) {
28+
if (accessToken) {
2929
const accessTokenData = await jwt.decodeToken<AccessTokenData>(accessToken)
3030

3131
const diff = accessTokenData.exp * 1000 - new Date().getTime()
3232
// refresh token when life < 30mins
3333
if (diff < Time.ONE_MINUTE_IN_MS * 30 && refreshToken) {
3434
await userService.restoreToken({ request, reply })
3535
}
36+
37+
request.user = { id: accessTokenData.user_id }
38+
return
3639
}
3740

3841
if (!accessToken && refreshToken) {
3942
const tokens = await userService.restoreToken({ request, reply })
4043
accessToken = tokens.accessToken
41-
}
4244

43-
if (!accessToken) return
45+
const accessTokenData = await jwt.decodeToken<AccessTokenData>(accessToken)
46+
request.user = { id: accessTokenData.user_id }
47+
return
48+
}
4449

45-
const accessTokenData = await jwt.decodeToken<AccessTokenData>(accessToken)
46-
request.user = { id: accessTokenData.user_id }
50+
request.user = null
4751
} catch (e) {
4852
console.log('accessToken', accessToken)
4953
console.log('authPlugin error', e)
@@ -55,10 +59,9 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
5559

5660
const accessTokenData = await jwt.decodeToken<AccessTokenData>(accessToken)
5761
request.user = { id: accessTokenData.user_id }
58-
} else {
59-
throw new Error()
6062
}
6163
} catch (error) {
64+
console.log('refresh token error', error)
6265
const cookie = container.resolve(CookieService)
6366
cookie.clearCookie(reply, 'access_token')
6467
cookie.clearCookie(reply, 'refresh_token')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class UserService implements Service {
123123
public async updateLastAccessedAt(userId?: string): Promise<void> {
124124
if (!userId) return
125125
try {
126-
await this.db.userProfile.update({
126+
await this.db.userProfile.updateMany({
127127
where: {
128128
fk_user_id: userId,
129129
},

packages/velog-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"html-react-parser": "^5.0.6",
4444
"inquirer": "^9.2.7",
4545
"nanoid": "^4.0.2",
46-
"next": "^14.1.0",
46+
"next": "14.1.0",
4747
"postcss-flexbugs-fixes": "^5.0.2",
4848
"postcss-preset-env": "^8.5.0",
4949
"prismjs": "^1.29.0",

packages/velog-web/src/graphql/helpers/generated.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ export type CreateNotificationInput = {
104104
type: NotificationType
105105
}
106106

107+
export type EditPostInput = {
108+
body: Scalars['String']['input']
109+
id: Scalars['ID']['input']
110+
is_markdown: Scalars['Boolean']['input']
111+
is_private: Scalars['Boolean']['input']
112+
is_temp: Scalars['Boolean']['input']
113+
meta: Scalars['JSON']['input']
114+
series_id?: InputMaybe<Scalars['ID']['input']>
115+
tags: Array<Scalars['String']['input']>
116+
thumbnail?: InputMaybe<Scalars['String']['input']>
117+
title: Scalars['String']['input']
118+
token?: InputMaybe<Scalars['String']['input']>
119+
url_slug: Scalars['String']['input']
120+
}
121+
107122
export type FeedPostsInput = {
108123
limit?: InputMaybe<Scalars['PositiveInt']['input']>
109124
offset?: InputMaybe<Scalars['Int']['input']>
@@ -187,16 +202,17 @@ export type Mutation = {
187202
acceptIntegration: Scalars['String']['output']
188203
confirmChangeEmail: Maybe<Scalars['Void']['output']>
189204
createNotification: Notification
205+
editPost: Post
190206
follow: Maybe<Scalars['Boolean']['output']>
191207
initiateChangeEmail: Maybe<Scalars['Void']['output']>
192-
likePost: Maybe<Post>
208+
likePost: Post
193209
logout: Maybe<Scalars['Void']['output']>
194210
readAllNotifications: Maybe<Scalars['Void']['output']>
195211
readNotification: Maybe<Scalars['Void']['output']>
196212
removeAllNotifications: Maybe<Scalars['Void']['output']>
197213
sendMail: Maybe<SendMailResponse>
198214
unfollow: Maybe<Scalars['Boolean']['output']>
199-
unlikePost: Maybe<Post>
215+
unlikePost: Post
200216
unregister: Maybe<Scalars['Void']['output']>
201217
updateAbout: Maybe<UserProfile>
202218
updateEmailRules: Maybe<UserMeta>
@@ -205,6 +221,7 @@ export type Mutation = {
205221
updateSocialInfo: Maybe<UserProfile>
206222
updateThumbnail: Maybe<UserProfile>
207223
updateVelogTitle: Maybe<VelogConfig>
224+
writePost: Post
208225
}
209226

210227
export type MutationConfirmChangeEmailArgs = {
@@ -215,6 +232,10 @@ export type MutationCreateNotificationArgs = {
215232
input: CreateNotificationInput
216233
}
217234

235+
export type MutationEditPostArgs = {
236+
input: EditPostInput
237+
}
238+
218239
export type MutationFollowArgs = {
219240
input: FollowInput
220241
}
@@ -271,6 +292,10 @@ export type MutationUpdateVelogTitleArgs = {
271292
input: UpdateVelogTitleInput
272293
}
273294

295+
export type MutationWritePostArgs = {
296+
input: WritePostInput
297+
}
298+
274299
export type Notification = {
275300
action: Scalars['JSON']['output']
276301
action_id: Maybe<Scalars['ID']['output']>
@@ -611,6 +636,7 @@ export type User = {
611636
id: Scalars['ID']['output']
612637
is_certified: Scalars['Boolean']['output']
613638
is_followed: Scalars['Boolean']['output']
639+
is_trusted: Scalars['Boolean']['output']
614640
profile: UserProfile
615641
series_list: Array<Series>
616642
updated_at: Scalars['DateTimeISO']['output']
@@ -656,6 +682,20 @@ export type VelogConfig = {
656682
title: Maybe<Scalars['String']['output']>
657683
}
658684

685+
export type WritePostInput = {
686+
body: Scalars['String']['input']
687+
is_markdown: Scalars['Boolean']['input']
688+
is_private: Scalars['Boolean']['input']
689+
is_temp: Scalars['Boolean']['input']
690+
meta: Scalars['JSON']['input']
691+
series_id?: InputMaybe<Scalars['ID']['input']>
692+
tags: Array<Scalars['String']['input']>
693+
thumbnail?: InputMaybe<Scalars['String']['input']>
694+
title: Scalars['String']['input']
695+
token?: InputMaybe<Scalars['String']['input']>
696+
url_slug: Scalars['String']['input']
697+
}
698+
659699
export type AdsQueryVariables = Exact<{
660700
input: AdsInput
661701
}>

packages/velog-web/src/prefetch/getCurrentUser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default async function getCurrentUser() {
66
try {
77
const headers = {}
88
const token = getAccessToken()
9+
910
if (token) {
1011
Object.assign(headers, { authorization: `Bearer ${token.value}` })
1112
}

0 commit comments

Comments
 (0)