import { Controller , Body , Post , UseGuards , HttpStatus } from '@nestjs/common' ; import { ApiResponse , ApiUseTags } from '@nestjs/swagger' ; import { AuthGuard } from '@nestjs/passport' ; import { AuthService } from './auth.service' ; import { LoginPayload } from './login.payload' ; import { RegisterPayload } from './register.payload' ; import { UsersService } from '../user/user.service' ; @ Controller ( 'auth' ) @ ApiUseTags ( 'authentication' ) export class AuthController { constructor ( private readonly authService , private readonly userService ) { } @ Post ( 'login' ) @ UseGuards ( AuthGuard ( ) ) @ ApiResponse ( { status : HttpStatus . ACCEPTED , description : 'Successful Login' } ) @ ApiResponse ( { status : HttpStatus . BAD_REQUEST , description : 'Bad Request' } ) @ ApiResponse ( { status : HttpStatus . UNAUTHORIZED , description : 'Unauthorized' } ) async login ( @ Body ( ) credentials ) { const user = await this . authService . validateUser ( credentials ) ; return await this . authService . generateToken ( user ) ; } @ Post ( 'register' ) @ ApiResponse ( { status : HttpStatus . ACCEPTED , description : 'Successful Registration' , } ) @ ApiResponse ( { status : HttpStatus . BAD_REQUEST , description : 'Bad Request' } ) @ ApiResponse ( { status : HttpStatus . UNAUTHORIZED , description : 'Unauthorized' } ) async register ( @ Body ( ) payload ) { const user = await this . userService . create ( payload ) ; return await this . authService . generateToken ( user ) ; } }