1- import { Controller , Get , HttpStatus , Post , Res , UseGuards } from '@nestjs/common'
1+ import { Body , Controller , Get , Header , Headers , HttpStatus , Post , Res , UseGuards } from '@nestjs/common'
22import { AuthService } from './auth.service'
33import { AbstractController } from '~/_common/abstracts/abstract.controller'
44import { Response } from 'express'
@@ -23,7 +23,7 @@ export class AuthController extends AbstractController {
2323 @Post ( 'local' )
2424 @UseGuards ( AuthGuard ( 'local' ) )
2525 public async authenticateWithLocal ( @Res ( ) res : Response , @ReqIdentity ( ) user : IdentityType ) : Promise < Response > {
26- const tokens = await this . service . createToken ( user )
26+ const tokens = await this . service . createTokens ( user )
2727 return res . status ( HttpStatus . OK ) . json ( {
2828 ...tokens ,
2929 user,
@@ -32,16 +32,24 @@ export class AuthController extends AbstractController {
3232
3333 @Get ( 'session' )
3434 @UseGuards ( AuthGuard ( 'jwt' ) )
35- public async session ( @Res ( ) res : Response , @ReqIdentity ( ) user : IdentityType ) : Promise < Response > {
36- const tokens = await this . service . createToken ( user )
35+ public async session ( @Res ( ) res : Response , @ReqIdentity ( ) identity : IdentityType ) : Promise < Response > {
36+ const user = await this . service . getSessionData ( identity )
3737 return res . status ( HttpStatus . OK ) . json ( {
38- ...tokens ,
3938 user,
4039 } )
4140 }
4241
42+ @Post ( 'refresh' )
43+ public async refresh ( @Res ( ) res : Response , @Body ( ) body : any ) : Promise < Response > {
44+ const tokens = await this . service . renewTokens ( body . refresh_token )
45+ return res . status ( HttpStatus . OK ) . json ( {
46+ ...tokens ,
47+ } )
48+ }
49+
4350 @Post ( 'logout' )
44- public async logout ( @Res ( ) res : Response ) : Promise < Response > {
51+ public async logout ( @Res ( ) res : Response , @Headers ( 'Authorization' ) jwt : string ) : Promise < Response > {
52+ await this . service . clearSession ( jwt . replace ( / ^ B e a r e r \s / , '' ) )
4553 return res . status ( HttpStatus . OK ) . send ( )
4654 }
4755}
0 commit comments