mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
add auth docs, add eslint import/order
This commit is contained in:
@@ -12,11 +12,21 @@ import {
|
||||
UnauthorizedException,
|
||||
UseGuards,
|
||||
} from '@nestjs/common'
|
||||
import { RegistrationDto } from './dto/registration.dto'
|
||||
import { LocalAuthGuard, JwtAuthGuard, JwtRefreshGuard } from './guards'
|
||||
import { Response as ExpressResponse } from 'express'
|
||||
import { Cookies } from '../../infrastructure/decorators'
|
||||
import { CommandBus } from '@nestjs/cqrs'
|
||||
import { ApiBody, ApiTags } from '@nestjs/swagger'
|
||||
import { Response as ExpressResponse } from 'express'
|
||||
|
||||
import { Cookies } from '../../infrastructure/decorators'
|
||||
|
||||
import {
|
||||
EmailVerificationDto,
|
||||
LoginDto,
|
||||
RecoverPasswordDto,
|
||||
RegistrationDto,
|
||||
ResendVerificationEmailDto,
|
||||
} from './dto'
|
||||
import { LoginResponse, UserEntity } from './entities/auth.entity'
|
||||
import { JwtAuthGuard, JwtRefreshGuard, LocalAuthGuard } from './guards'
|
||||
import {
|
||||
CreateUserCommand,
|
||||
GetCurrentUserDataCommand,
|
||||
@@ -28,22 +38,29 @@ import {
|
||||
VerifyEmailCommand,
|
||||
} from './use-cases'
|
||||
|
||||
@ApiTags('Auth')
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private commandBus: CommandBus) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('me')
|
||||
async getUserData(@Request() req) {
|
||||
async getUserData(@Request() req): Promise<UserEntity> {
|
||||
const userId = req.user.id
|
||||
|
||||
return await this.commandBus.execute(new GetCurrentUserDataCommand(userId))
|
||||
}
|
||||
|
||||
@HttpCode(200)
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiBody({ type: LoginDto })
|
||||
@UseGuards(LocalAuthGuard)
|
||||
@Post('login')
|
||||
async login(@Request() req, @Res({ passthrough: true }) res: ExpressResponse) {
|
||||
async login(
|
||||
@Request() req,
|
||||
@Res({ passthrough: true }) res: ExpressResponse
|
||||
): Promise<LoginResponse> {
|
||||
const userData = req.user.data
|
||||
|
||||
res.cookie('refreshToken', userData.refreshToken, {
|
||||
httpOnly: true,
|
||||
sameSite: 'none',
|
||||
@@ -55,63 +72,77 @@ export class AuthController {
|
||||
sameSite: 'none',
|
||||
secure: true,
|
||||
})
|
||||
|
||||
return { accessToken: req.user.data.accessToken }
|
||||
}
|
||||
|
||||
@HttpCode(201)
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@Post('sign-up')
|
||||
async registration(@Body() registrationData: RegistrationDto) {
|
||||
async registration(@Body() registrationData: RegistrationDto): Promise<UserEntity> {
|
||||
return await this.commandBus.execute(new CreateUserCommand(registrationData))
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@Post('verify-email')
|
||||
async confirmRegistration(@Body('code') confirmationCode) {
|
||||
return await this.commandBus.execute(new VerifyEmailCommand(confirmationCode))
|
||||
async confirmRegistration(@Body() body: EmailVerificationDto): Promise<void> {
|
||||
return await this.commandBus.execute(new VerifyEmailCommand(body.code))
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@Post('resend-verification-email')
|
||||
async resendVerificationEmail(@Body('userId') userId: string) {
|
||||
return await this.commandBus.execute(new ResendVerificationEmailCommand(userId))
|
||||
async resendVerificationEmail(@Body() body: ResendVerificationEmailDto): Promise<void> {
|
||||
return await this.commandBus.execute(new ResendVerificationEmailCommand(body.userId))
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('logout')
|
||||
async logout(
|
||||
@Cookies('refreshToken') refreshToken: string,
|
||||
@Res({ passthrough: true }) res: ExpressResponse
|
||||
) {
|
||||
): Promise<void> {
|
||||
if (!refreshToken) throw new UnauthorizedException()
|
||||
await this.commandBus.execute(new LogoutCommand(refreshToken))
|
||||
res.clearCookie('refreshToken')
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@UseGuards(JwtRefreshGuard)
|
||||
@Get('refresh-token')
|
||||
async refreshToken(@Request() req, @Response({ passthrough: true }) res: ExpressResponse) {
|
||||
@Post('refresh-token')
|
||||
async refreshToken(
|
||||
@Request() req,
|
||||
@Response({ passthrough: true }) res: ExpressResponse
|
||||
): Promise<LoginResponse> {
|
||||
if (!req.cookies?.refreshToken) throw new UnauthorizedException()
|
||||
const userId = req.user.id
|
||||
const newTokens = await this.commandBus.execute(new RefreshTokenCommand(userId))
|
||||
|
||||
res.cookie('refreshToken', newTokens.refreshToken, {
|
||||
httpOnly: true,
|
||||
// secure: true,
|
||||
path: '/v1/auth/refresh-token',
|
||||
expires: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
|
||||
})
|
||||
|
||||
return {
|
||||
accessToken: newTokens.accessToken,
|
||||
}
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@Post('recover-password')
|
||||
async recoverPassword(@Body('email') email: string) {
|
||||
return await this.commandBus.execute(new SendPasswordRecoveryEmailCommand(email))
|
||||
async recoverPassword(@Body() body: RecoverPasswordDto): Promise<void> {
|
||||
return await this.commandBus.execute(new SendPasswordRecoveryEmailCommand(body.email))
|
||||
}
|
||||
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@Post('reset-password/:token')
|
||||
async resetPassword(@Body('password') password: string, @Param('token') token: string) {
|
||||
async resetPassword(
|
||||
@Body('password') password: string,
|
||||
@Param('token') token: string
|
||||
): Promise<void> {
|
||||
return await this.commandBus.execute(new ResetPasswordCommand(token, password))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user