import { Controller , Headers , Post , Body , UseGuards , Get , Patch , Query , Request , Res , BadRequestException } from '@nestjs/common' ; import { AuthGuard } from '@nestjs/passport' ; import { ApiTags , ApiBearerAuth } from '@nestjs/swagger' ; import { RateLimit } from 'nestjs-rate-limiter' ; import { AuthService } from '../Service/auth.service' ; import { CreateAccountDto } from '../Dto/create-account.dto' ; import { LoginDto } from '../Dto/login.dto' ; import { GenerateRecoveryKeyDto } from '../Dto/generate-recovery-key.dto' ; import { RecoverAccountDto } from '../Dto/recover-account.dto' ; import { ISerializeResponse } from 'src/shared/Services/serializer.service' ; import { configService } from 'src/shared/Services/config.service' ; import { StatusOk } from 'src/shared/Types' ; @ ApiTags ( 'v1/auth' ) @ Controller ( ) export class AuthController { constructor ( private readonly authService ) { } @ RateLimit ( { points : 5 , duration : 300 , errorMessage : 'You have reached the limit of login requests. You have to wait 5 minutes before trying again.' } ) @ Post ( 'signin' ) async signIn ( @ Body ( ) dto , @ Res ( ) res ) { const user = await this . authService . validateUser ( dto ) ; const authResponse = await this . authService . signIn ( user , dto ) ; const refreshToken = authResponse . attributes . user . refresh_token ; delete authResponse . attributes . user . refresh_token ; if ( dto . rememberMe ) { res . setCookie ( 'rt' , refreshToken , { domain : `${ configService . getEnv ( 'APP_DOMAIN' ) . split ( '//' ) [ 1 ] }` , path : '/api/v1/auth/refresh-token' , httpOnly : true , secure : true } ) . send ( authResponse ) ; return ; } res . send ( authResponse ) ; } @ RateLimit ( { points : 1 , duration : 120 , errorMessage : 'You have reached the limit. You have to wait 2 minutes before trying again.' } ) @ Post ( 'signup' ) signUp ( @ Body ( ) dto ) { return this . authService . signUp ( dto ) ; } @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ( 'jwt' ) ) @ Get ( 'signout' ) async signOut ( @ Headers ( 'authorization' ) bearer ) { return this . authService . signOut ( bearer ) ; } @ RateLimit ( { points : 3 , duration : 300 , errorMessage : 'You have reached the limit. You have to wait 5 minutes before trying again.' } ) @ Patch ( 'generate-recovery-key' ) generateRecoveryKey ( @ Body ( ) dto ) { return this . authService . generateRecoveryKey ( dto ) ; } @ RateLimit ( { points : 3 , duration : 300 , errorMessage : 'You have reached the limit. You have to wait 5 minutes before trying again.' } ) @ Patch ( 'recover-account' ) recoverAccount ( @ Body ( ) dto ) { return this . authService . recoverAccount ( dto ) ; } @ RateLimit ( { points : 3 , duration : 300 , errorMessage : 'You have reached the limit. You have to wait 5 minutes before trying again.' } ) @ Get ( 'account-verification' ) accountVerification ( @ Query ( 'token' ) token ) { return this . authService . accountVerification ( token ) ; } @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ( 'jwt' ) ) @ Get ( 'check-token' ) async checkJwtToken ( ) { return { status : 'ok' , message : 'Token is valid' } ; } @ Get ( 'refresh-token' ) async refreshJwtToken ( @ Request ( ) { cookies } ) { if ( ! cookies . rt ) throw new BadRequestException ( 'Server can not give access token without a refresh token' ) ; return await this . authService . refreshToken ( cookies . rt ) ; } }