2
2
Body ,
3
3
Controller ,
4
4
Get ,
5
- InternalServerErrorException ,
6
5
Post ,
7
6
Req ,
8
7
Res ,
@@ -39,34 +38,23 @@ export class AuthController {
39
38
@Body ( ) body : GithubAuthenticationRequestDto ,
40
39
@Res ( ) response : Response ,
41
40
) {
42
- try {
43
- const result = await this . authService . githubAuthentication ( body . authCode ) ;
44
- if ( 'accessToken' in result && 'refreshToken' in result ) {
45
- const { accessToken, refreshToken } = result ;
46
- const { username, githubImageUrl } =
47
- await this . memberService . getMemberPublicInfo ( accessToken ) ;
48
- const responseBody = {
49
- accessToken,
50
- member : { username, imageUrl : githubImageUrl } ,
51
- } ;
52
- return response
53
- . status ( 201 )
54
- . cookie ( 'refreshToken' , refreshToken , this . cookieOptions )
55
- . send ( responseBody ) ;
56
- } else if ( 'tempIdToken' in result ) {
57
- const responseBody = { tempIdToken : result . tempIdToken } ;
58
- return response . status ( 209 ) . send ( responseBody ) ;
59
- } else
60
- throw new Error ( 'assert: unexpected authentication result returned' ) ;
61
- } catch ( err ) {
62
- if (
63
- err . message === 'Cannot retrieve access token' ||
64
- err . message === 'Cannot retrieve github user'
65
- ) {
66
- throw new UnauthorizedException ( 'Invalid authorization code' ) ;
67
- }
68
- throw new InternalServerErrorException ( err . message ) ;
69
- }
41
+ const result = await this . authService . githubAuthentication ( body . authCode ) ;
42
+ if ( 'accessToken' in result && 'refreshToken' in result ) {
43
+ const { accessToken, refreshToken } = result ;
44
+ const { username, githubImageUrl } =
45
+ await this . memberService . getMemberPublicInfo ( accessToken ) ;
46
+ const responseBody = {
47
+ accessToken,
48
+ member : { username, imageUrl : githubImageUrl } ,
49
+ } ;
50
+ return response
51
+ . status ( 201 )
52
+ . cookie ( 'refreshToken' , refreshToken , this . cookieOptions )
53
+ . send ( responseBody ) ;
54
+ } else if ( 'tempIdToken' in result ) {
55
+ const responseBody = { tempIdToken : result . tempIdToken } ;
56
+ return response . status ( 209 ) . send ( responseBody ) ;
57
+ } else throw new Error ( 'assert: unexpected authentication result returned' ) ;
70
58
}
71
59
72
60
@Post ( 'github/signup' )
@@ -78,32 +66,24 @@ export class AuthController {
78
66
const tempIdToken = request . token ;
79
67
if ( ! tempIdToken )
80
68
throw new UnauthorizedException ( 'Bearer Token is missing' ) ;
81
- try {
82
- const { accessToken, refreshToken } = await this . authService . githubSignup (
83
- tempIdToken ,
84
- body . username ,
85
- body . position ,
86
- { stacks : body . techStack } ,
87
- ) ;
88
- const { username, githubImageUrl } =
89
- await this . memberService . getMemberPublicInfo ( accessToken ) ;
90
- const responseBody = {
91
- accessToken,
92
- member : { username, imageUrl : githubImageUrl } ,
93
- } ;
94
- return response
95
- . status ( 201 )
96
- . cookie ( 'refreshToken' , refreshToken , this . cookieOptions )
97
- . send ( responseBody ) ;
98
- } catch ( err ) {
99
- if (
100
- err . message === 'Failed to verify token' ||
101
- err . message === 'tempIdToken does not match'
102
- ) {
103
- throw new UnauthorizedException ( 'Expired:tempIdToken' ) ;
104
- }
105
- throw new InternalServerErrorException ( err . message ) ;
106
- }
69
+
70
+ const { accessToken, refreshToken } = await this . authService . githubSignup (
71
+ tempIdToken ,
72
+ body . username ,
73
+ body . position ,
74
+ { stacks : body . techStack } ,
75
+ ) ;
76
+ const { username, githubImageUrl } =
77
+ await this . memberService . getMemberPublicInfo ( accessToken ) ;
78
+ const responseBody = {
79
+ accessToken,
80
+ member : { username, imageUrl : githubImageUrl } ,
81
+ } ;
82
+
83
+ return response
84
+ . status ( 201 )
85
+ . cookie ( 'refreshToken' , refreshToken , this . cookieOptions )
86
+ . send ( responseBody ) ;
107
87
}
108
88
109
89
@Post ( 'logout' )
@@ -112,17 +92,8 @@ export class AuthController {
112
92
if ( ! accessToken )
113
93
throw new UnauthorizedException ( 'Bearer Token is missing' ) ;
114
94
115
- try {
116
- await this . authService . logout ( accessToken ) ;
117
- } catch ( err ) {
118
- if ( err . message === 'Not a logged in member' ) {
119
- throw new UnauthorizedException ( 'Not a logged in member' ) ;
120
- }
121
- if ( err . message === 'Failed to verify token' ) {
122
- throw new UnauthorizedException ( 'Expired:accessToken' ) ;
123
- }
124
- throw new InternalServerErrorException ( err . message ) ;
125
- }
95
+ await this . authService . logout ( accessToken ) ;
96
+
126
97
return response
127
98
. clearCookie ( 'refreshToken' , this . cookieOptions )
128
99
. status ( 200 )
@@ -135,43 +106,27 @@ export class AuthController {
135
106
if ( ! requestRefreshToken ) {
136
107
throw new UnauthorizedException ( 'Refresh Token is missing' ) ;
137
108
}
138
- try {
139
- const { accessToken, refreshToken } =
140
- await this . authService . refreshAccessTokenAndRefreshToken (
141
- requestRefreshToken ,
142
- ) ;
143
- return response
144
- . status ( 201 )
145
- . cookie ( 'refreshToken' , refreshToken , this . cookieOptions )
146
- . send ( { accessToken } ) ;
147
- } catch ( err ) {
148
- if (
149
- err . message === 'No matching refresh token' ||
150
- err . message === 'Failed to verify token'
151
- ) {
152
- throw new UnauthorizedException ( 'Expired:refreshToken' ) ;
153
- }
154
- throw new InternalServerErrorException ( err . message ) ;
155
- }
109
+
110
+ const { accessToken, refreshToken } =
111
+ await this . authService . refreshAccessTokenAndRefreshToken (
112
+ requestRefreshToken ,
113
+ ) ;
114
+
115
+ return response
116
+ . status ( 201 )
117
+ . cookie ( 'refreshToken' , refreshToken , this . cookieOptions )
118
+ . send ( { accessToken } ) ;
156
119
}
157
120
158
121
@Get ( '/github/username' )
159
122
async getGithubUsername ( @Req ( ) request : BearerTokenRequest ) {
160
123
const tempIdToken = request . token ;
161
124
if ( ! tempIdToken )
162
125
throw new UnauthorizedException ( 'Bearer Token is missing' ) ;
163
- try {
164
- const githubUsername =
165
- await this . authService . getGithubUsernameByTempIdToken ( tempIdToken ) ;
166
- return { githubUsername } ;
167
- } catch ( err ) {
168
- if (
169
- err . message === 'Failed to verify token' ||
170
- err . message === 'tempIdToken does not match'
171
- ) {
172
- throw new UnauthorizedException ( 'Expired:tempIdToken' ) ;
173
- }
174
- throw new InternalServerErrorException ( err . message ) ;
175
- }
126
+
127
+ const githubUsername =
128
+ await this . authService . getGithubUsernameByTempIdToken ( tempIdToken ) ;
129
+
130
+ return { githubUsername } ;
176
131
}
177
132
}
0 commit comments