11import httpStatus from 'http-status' ;
22import { Body , Controller , Post , Request , Route , Security } from 'tsoa' ;
3+
34import { AuthService } from 'services/auth' ;
45import {
56 CreateUserParams ,
@@ -8,27 +9,48 @@ import {
89 AuthenticatedRequest ,
910 LoginParams ,
1011} from 'types' ;
12+ import { cookieEnabled , JWTEnabled } from 'config/config' ;
13+ import { COOKIE_NAME , cookieConfig } from 'utils/auth' ;
1114
1215@Route ( 'v1/auth' )
1316export class AuthControllerV1 extends Controller {
1417 @Post ( '/register' )
15- public async register ( @Body ( ) user : CreateUserParams ) : Promise < ReturnAuth > {
16- const authReturn = await AuthService . register ( user ) ;
18+ public async register (
19+ @Body ( ) user : CreateUserParams ,
20+ @Request ( ) req : AuthenticatedRequest ,
21+ ) : Promise < ReturnAuth | null > {
22+ const { sessionId, ...authReturn } = await AuthService . register ( user ) ;
23+ const { res } = req ;
24+ if ( cookieEnabled ) {
25+ res ?. cookie ( COOKIE_NAME , sessionId , cookieConfig ) ;
26+ }
1727 this . setStatus ( httpStatus . CREATED ) ;
18- return authReturn ;
28+ if ( JWTEnabled ) return authReturn ;
29+ return null ;
1930 }
2031
2132 @Post ( '/login' )
22- public async login ( @Body ( ) loginParams : LoginParams ) : Promise < ReturnAuth > {
23- const authReturn = await AuthService . login ( loginParams ) ;
33+ public async login (
34+ @Body ( ) loginParams : LoginParams ,
35+ @Request ( ) req : AuthenticatedRequest ,
36+ ) : Promise < ReturnAuth | null > {
37+ const { sessionId, ...authReturn } = await AuthService . login ( loginParams ) ;
38+ const { res } = req ;
39+ if ( cookieEnabled ) {
40+ res ?. cookie ( COOKIE_NAME , sessionId , cookieConfig ) ;
41+ }
2442 this . setStatus ( httpStatus . OK ) ;
25- return authReturn ;
43+ if ( JWTEnabled ) return authReturn ;
44+ return null ;
2645 }
2746
2847 @Post ( '/logout' )
48+ @Security ( 'cookie' )
2949 @Security ( 'jwt' )
3050 public async logout ( @Request ( ) req : AuthenticatedRequest ) : Promise < void > {
3151 await AuthService . logout ( req . user . token ) ;
52+ const { res } = req ;
53+ res ?. clearCookie ( COOKIE_NAME ) ;
3254 this . setStatus ( httpStatus . OK ) ;
3355 }
3456
0 commit comments