import { Controller, UseGuards, Post, Request, Get, Body, UseInterceptors, } from '@nestjs/common'; import { AuthService } from './auth.service'; import { LocalAuthGuard } from './local-auth.guard'; import { JwtAuthGuard } from './jwt-auth.guard'; import { ApiTags, ApiBearerAuth, ApiHeader, ApiBody, ApiProperty, } from '@nestjs/swagger'; import { UserDto } from '../users/user.dto'; import { PasswordToHashInterceptor } from '../users/password-to-hash.interceptor'; import { ForgotPasswordDto } from './forgot-password.dto'; import { SetPasswordDto } from './set-password.dto'; export class Login { @ApiProperty() email: string; @ApiProperty() password: string; } @ApiTags('Auth') @Controller('auth') export class AuthController { constructor(private authService: AuthService) {} @UseGuards(LocalAuthGuard) @Post(['login', 'signin']) @ApiBody({ type: Login }) async login(@Request() req) { return this.authService.login(req.user); } @UseInterceptors(PasswordToHashInterceptor) @Post('debug-user-create') @ApiBody({ type: UserDto }) async debugUserCreate(@Body() user: UserDto) { return await this.authService.debugUserCreate(user); } @ApiHeader({ name: 'Authorization', allowEmptyValue: false, description: '"Bearer Token"', example: 'Bearer ', }) @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('profile') getProfile(@Request() req) { return req.user; } @Post('forgot') @ApiBody({ type: ForgotPasswordDto }) async forgotPassword(@Body() json: ForgotPasswordDto) { const { email } = json; return await this.authService.forgotPassword(email); } @UseInterceptors(PasswordToHashInterceptor) @Post('set-password') @ApiBody({ type: SetPasswordDto }) async setPassword(@Body() json: SetPasswordDto) { const { passwordResetToken, passwordHash } = json; return await this.authService.setPassword(passwordResetToken, passwordHash); } }