import { Body , Param , Post , Res , UseGuards , Controller , Get , Logger , Req , UseInterceptors , ClassSerializerInterceptor , InternalServerErrorException , } from '@nestjs/common' ; import { Response , Request } from 'express' ; import { AuthGuard , Roles , RoleType , RolesGuard } from '../../security' ; import { PasswordChangeDTO } from '../../service/dto/password-change.dto' ; import { UserDTO } from '../../service/dto/user.dto' ; import { LoggingInterceptor } from '../../client/interceptors/logging.interceptor' ; import { ApiBearerAuth , ApiUseTags , ApiResponse , ApiOperation } from '@nestjs/swagger' ; import { AuthService } from '../../service/auth.service' ; @ Controller ( 'api' ) @ UseInterceptors ( LoggingInterceptor , ClassSerializerInterceptor ) @ ApiUseTags ( 'account-resource' ) export class AccountController { logger = new Logger ( 'AccountController' ) ; constructor ( private readonly authService ) { } @ Post ( '/register' ) @ ApiOperation ( { title : 'Register user' } ) @ ApiResponse ( { status : 201 , description : 'Registered user' , type : UserDTO , } ) async registerAccount ( @ Req ( ) req , @ Body ( ) userDTO ) { return await this . authService . registerNewUser ( userDTO ) ; } @ Get ( '/activate' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard , RolesGuard ) @ Roles ( RoleType . ADMIN ) @ ApiOperation ( { title : 'Activate an account' } ) @ ApiResponse ( { status : 200 , description : 'activated' , } ) activateAccount ( @ Param ( ) key , @ Res ( ) res ) { throw new InternalServerErrorException ( ) ; } @ Get ( '/authenticate' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ) @ ApiOperation ( { title : 'Check if the user is authenticated' } ) @ ApiResponse ( { status : 200 , description : 'login authenticated' , } ) isAuthenticated ( @ Req ( ) req ) { const user = req . user ; return user . login ; } @ Get ( '/account' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ) @ ApiOperation ( { title : 'Get the current user.' } ) @ ApiResponse ( { status : 200 , description : 'user retrieved' , } ) async getAccount ( @ Req ( ) req ) { const user = req . user ; const userProfileFound = await this . authService . getAccount ( user . id ) ; return userProfileFound ; } @ Post ( '/account' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ) @ ApiOperation ( { title : 'Update the current user information' } ) @ ApiResponse ( { status : 201 , description : 'user info updated' , type : UserDTO , } ) async saveAccount ( @ Req ( ) req , @ Body ( ) newUserInfo ) { const user = req . user ; return await this . authService . updateUserSettings ( user . login , newUserInfo ) ; } @ Post ( '/account/change-password' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ) @ ApiOperation ( { title : 'Change current password' } ) @ ApiResponse ( { status : 201 , description : 'user password changed' , type : PasswordChangeDTO , } ) async changePassword ( @ Req ( ) req , @ Body ( ) passwordChange ) { const user = req . user ; return await this . authService . changePassword ( user . login , passwordChange . currentPassword , passwordChange . newPassword ) ; } @ Post ( '/account/reset-password/init' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ) @ ApiOperation ( { title : 'Send an email to reset the password of the user' } ) @ ApiResponse ( { status : 201 , description : 'mail to reset password sent' , type : 'string' , } ) requestPasswordReset ( @ Req ( ) req , @ Body ( ) email , @ Res ( ) res ) { throw new InternalServerErrorException ( ) ; } @ Post ( '/account/reset-password/finish' ) @ ApiBearerAuth ( ) @ UseGuards ( AuthGuard ) @ ApiOperation ( { title : 'Finish to reset the password of the user' } ) @ ApiResponse ( { status : 201 , description : 'password reset' , type : 'string' , } ) finishPasswordReset ( @ Req ( ) req , @ Body ( ) keyAndPassword , @ Res ( ) res ) { throw new InternalServerErrorException ( ) ; } }