import { Req , Controller , Get , Res , Query , Inject } from '@nestjs/common' ; import { ConfigType } from '@nestjs/config' ; import { Request , Response } from 'express' ; import { baseConfig } from '../../../config/base.config' ; import { JwtAuthService } from './jwt-auth.service' ; @ Controller ( 'jwt' ) export class JwtAuthController { constructor ( private readonly jwtEnv , @ Inject ( baseConfig . KEY ) private readonly baseEnv ) { } @ Get ( 'refresh' ) refresh ( @ Req ( ) req , @ Res ( ) res , @ Query ( 'redirect' ) redirect ? ) { const oldRefreshToken = req . cookies [ 'refresh_token' ] ; try { const { uid } = this . jwtEnv . verifyRefreshToken ( oldRefreshToken ) ; const { accessToken , refreshToken } = this . jwtEnv . createToken ( uid ) ; this . jwtEnv . setAccessToken ( res , accessToken ) ; this . jwtEnv . setRefreshToken ( res , refreshToken ) ; if ( redirect ) { return res . redirect ( `${ this . baseEnv . clientHost }${ redirect }` ) ; } return res . json ( { accessToken } ) ; } catch ( error ) { this . jwtEnv . removeToken ( res ) ; if ( redirect ) { return res . redirect ( `${ this . baseEnv . clientHost }/login` ) ; } return res . json ( { accessToken : null } ) ; } } @ Get ( 'logout' ) logout ( @ Res ( ) res ) { this . jwtEnv . removeToken ( res ) ; return res . json ( { ok : true } ) ; } }