1- import { Controller , Get , HttpCode , Param , Query , Res } from '@nestjs/common' ;
21import { Response } from 'express' ;
2+ import {
3+ Controller ,
4+ Get ,
5+ HttpCode ,
6+ Param ,
7+ Query ,
8+ Redirect ,
9+ Req ,
10+ Res ,
11+ UseGuards ,
12+ } from '@nestjs/common' ;
313import { AuthService } from '../service/auth.service' ;
14+ import { Request , Response } from 'express' ;
15+ import {
16+ ACCESS_TOKEN ,
17+ JWT_ACCESS_TOKEN_EXPIRATION_TIME ,
18+ JWT_ACCESS_TOKEN_SECRET ,
19+ JWT_REFRESH_TOKEN_EXPIRATION_TIME ,
20+ JWT_REFRESH_TOKEN_SECRET ,
21+ OK ,
22+ REFRESH_TOKEN ,
23+ tokenCookieOptions ,
24+ } from '@constant' ;
25+ import { JwtAuthGuard } from '../guard/jwt.guard' ;
426
527@Controller ( 'auth' )
628export class AuthController {
@@ -14,8 +36,43 @@ export class AuthController {
1436 }
1537
1638 @Get ( 'oauth/callback/:type' )
17- async socialStart ( @Query ( 'code' ) authorizationCode : string , @Param ( 'type' ) type : string ) {
18- const userId = await this . authService . socialStart ( { type, authorizationCode } ) ;
19- return userId ;
39+ async socialStart (
40+ @Query ( 'code' ) authorizationCode : string ,
41+ @Param ( 'type' ) type : string ,
42+ @Res ( { passthrough : true } ) res : Response
43+ ) {
44+ const user = await this . authService . socialStart ( { type, authorizationCode } ) ;
45+ const accessToken = this . authService . createJwt ( {
46+ payload : { nickname : 'user.nickname' , email : 'user.email' } ,
47+ secret : JWT_ACCESS_TOKEN_SECRET ,
48+ expirationTime : JWT_ACCESS_TOKEN_EXPIRATION_TIME ,
49+ } ) ;
50+ const refreshToken = this . authService . createJwt ( {
51+ payload : { nickname : 'user.nickname' , email : 'user.email' } ,
52+ secret : JWT_REFRESH_TOKEN_SECRET ,
53+ expirationTime : JWT_REFRESH_TOKEN_EXPIRATION_TIME ,
54+ } ) ;
55+
56+ res . cookie ( ACCESS_TOKEN , accessToken , tokenCookieOptions ) ;
57+ res . cookie ( REFRESH_TOKEN , refreshToken , tokenCookieOptions ) ;
58+ }
59+
60+ @UseGuards ( JwtAuthGuard )
61+ @Get ( 'login' )
62+ @HttpCode ( OK )
63+ loginValidate ( @Req ( ) req : Request , @Res ( { passthrough : true } ) res : Response ) {
64+ const { accessToken, refreshToken } = req . cookies ;
65+ res . cookie ( ACCESS_TOKEN , accessToken , tokenCookieOptions ) . cookie (
66+ REFRESH_TOKEN ,
67+ refreshToken ,
68+ tokenCookieOptions
69+ ) ;
70+ }
71+
72+ @UseGuards ( JwtAuthGuard )
73+ @Get ( 'logout' )
74+ logout ( @Res ( { passthrough : true } ) res : Response ) {
75+ res . clearCookie ( ACCESS_TOKEN ) ;
76+ res . clearCookie ( REFRESH_TOKEN ) ;
2077 }
2178}
0 commit comments