import { ApiException } from '@nanogiants/nestjs-swagger-api-exception-decorator' ; import { Body , Controller , Get , HttpCode , HttpStatus , Post , UploadedFile , Version , } from '@nestjs/common' ; import { ApiOkResponse , ApiTags } from '@nestjs/swagger' ; import { RoleType } from '../../constants' ; import { ApiFile , Auth , AuthUser } from '../../decorators' ; import { UserNotFoundException } from '../../exceptions' ; import { IFile } from '../../interfaces' ; import { UserDto } from '../user/dtos/user.dto' ; import { UserEntity } from '../user/user.entity' ; import { UserService } from '../user/user.service' ; import { AuthService } from './auth.service' ; import { LoginPayloadDto } from './dto/LoginPayloadDto' ; import { UserLoginDto } from './dto/UserLoginDto' ; import { UserRegisterDto } from './dto/UserRegisterDto' ; @ Controller ( 'auth' ) @ ApiTags ( 'auth' ) export class AuthController { constructor ( private userService , private authService ) { } @ Post ( 'login' ) @ HttpCode ( HttpStatus . OK ) @ ApiOkResponse ( { type : LoginPayloadDto , description : 'User info with access token' , } ) @ ApiException ( ( ) => [ UserNotFoundException ] ) async userLogin ( @ Body ( ) userLoginDto ) { const userEntity = await this . authService . validateUser ( userLoginDto ) ; const token = await this . authService . createAccessToken ( { userId : userEntity . id , role : userEntity . role , } ) ; return new LoginPayloadDto ( userEntity . toDto ( ) , token ) ; } @ Post ( 'register' ) @ HttpCode ( HttpStatus . OK ) @ ApiOkResponse ( { type : UserDto , description : 'Successfully Registered' } ) @ ApiFile ( { name : 'avatar' } ) async userRegister ( @ Body ( ) userRegisterDto , @ UploadedFile ( ) file ) { const createdUser = await this . userService . createUser ( userRegisterDto , file ) ; return createdUser . toDto ( { isActive : true , } ) ; } @ Version ( '1' ) @ Get ( 'me' ) @ HttpCode ( HttpStatus . OK ) @ Auth ( [ RoleType . USER , RoleType . ADMIN ] ) @ ApiOkResponse ( { type : UserDto , description : 'current user info' } ) getCurrentUser ( @ AuthUser ( ) user ) { return user . toDto ( ) ; } }