import { Controller , Request , Post , Get , Param , UseGuards , Logger , Body , } from '@nestjs/common' ; import { ApiBadRequestResponse , ApiCreatedResponse , ApiOkResponse , ApiOperation , ApiTags , ApiConflictResponse , } from '@nestjs/swagger' ; import { LocalAuthGuard } from './local-auth.guard' ; import { AuthService } from './auth.service' ; import { RegisterDto } from './dto/register.dto' ; import { UsersService } from '../users/users.service' ; import { TokenDto } from './dto/token.dto' ; import { LoginDto } from './dto/login.dto' ; import { EmailPasswordResetDto } from './dto/email-password-reset.dto' ; import { BackendErrorResponse } from '../../errorHandling/BackendErrorEntry.model' ; @ ApiTags ( 'Auth' ) @ Controller ( 'auth' ) @ ApiBadRequestResponse ( { description : 'Bad Request' , type : BackendErrorResponse , } ) @ ApiConflictResponse ( { description : 'Conflict' , type : ( ) => BackendErrorResponse , } ) export class AuthController { private readonly logger = new Logger ( AuthController . name ) ; constructor ( private authService , private usersService ) { } @ UseGuards ( LocalAuthGuard ) @ Post ( 'login' ) @ ApiOperation ( { summary : 'Login by email and password ' } ) @ ApiOkResponse ( { description : 'Successful Login' , type : TokenDto , } ) async login ( @ Request ( ) req , @ Body ( ) loginDto ) { this . logger . log ( `User login` ) ; return this . authService . createToken ( req . user ) ; } @ Post ( 'register' ) @ ApiOperation ( { summary : 'Register with email and password ' } ) @ ApiCreatedResponse ( { description : 'Successful Registration' , type : TokenDto , } ) async register ( @ Body ( ) payload ) { const user = await this . usersService . create ( payload ) ; this . logger . log ( `User registered: ${ user . id }` ) ; this . logger . debug ( `Email: ${ payload . email }` ) ; return this . authService . createToken ( user ) ; } @ Post ( 'refresh' ) @ ApiOperation ( { summary : 'Not yet implemented, token refresh' } ) @ ApiCreatedResponse ( { description : 'Successful token refresh' , type : TokenDto , } ) async refreshToken ( @ Body ( ) token ) { return ; } @ Get ( 'reset_email_password_initiate/:email' ) @ ApiOperation ( { summary : 'Email password reset initiation' } ) @ ApiCreatedResponse ( { description : 'If the email address exists, password reset was successfully triggered' , } ) async resetEmailPasswordInitiate ( @ Param ( 'email' ) email ) { const passwordResetToken = await this . usersService . createPasswordResetToken ( email ) ; if ( passwordResetToken !== null ) { } return ; } @ Post ( 'reset_email_password_complete' ) @ ApiOperation ( { summary : 'Email password reset initiation' } ) @ ApiCreatedResponse ( { description : 'If the email address exists, password reset was successfully triggered' , } ) async resetPasswordComplete ( @ Body ( ) payload ) { const user = await this . usersService . updatePasswordIfResetTokenMatches ( payload ) ; return this . authService . createToken ( user ) ; } }