import { Body , Controller , Get , Post , UseGuards } from '@nestjs/common' ; import { AuthGuard } from '@nestjs/passport' ; import { ApiBearerAuth , ApiResponse , ApiTags } from '@nestjs/swagger' ; import { AuthService , LoginPayload , RegisterPayload } from './' ; import { CurrentUser } from './../common/decorator/current-user.decorator' ; import { User , UsersService } from './../user' ; @ Controller ( 'api/auth' ) @ ApiTags ( 'authentication' ) export class AuthController { constructor ( private readonly authService , private readonly userService ) { } @ Post ( 'login' ) @ ApiResponse ( { status : 201 , description : 'Successful Login' } ) @ ApiResponse ( { status : 400 , description : 'Bad Request' } ) @ ApiResponse ( { status : 401 , description : 'Unauthorized' } ) async login ( @ Body ( ) payload ) { const user = await this . authService . validateUser ( payload ) ; return await this . authService . createToken ( user ) ; } @ Post ( 'register' ) @ ApiResponse ( { status : 201 , description : 'Successful Registration' } ) @ ApiResponse ( { status : 400 , description : 'Bad Request' } ) @ ApiResponse ( { status : 401 , description : 'Unauthorized' } ) async register ( @ Body ( ) payload ) { const user = await this . userService . create ( payload ) ; return await this . authService . createToken ( user ) ; } @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ( ) ) @ Get ( 'me' ) @ ApiResponse ( { status : 200 , description : 'Successful Response' } ) @ ApiResponse ( { status : 401 , description : 'Unauthorized' } ) async getLoggedInUser ( @ CurrentUser ( ) user ) { return user ; } }