From bf38ec5355273a1fa1e478324951b1108a4c7995 Mon Sep 17 00:00:00 2001 From: Pravdin Egor Date: Tue, 19 Sep 2023 21:17:00 +0700 Subject: [PATCH] auth swagger spec --- .dockerignore | 1 + src/auth/auth.controller.ts | 20 ++++++++++++++++++-- src/auth/dto/login.dto.ts | 14 ++++++++++++++ src/auth/dto/login.response.dto.ts | 3 +++ src/auth/dto/register.response.dto.ts | 12 ++++++++++++ src/auth/strategies/local.strategy.ts | 4 ++-- src/main.ts | 6 ++++-- src/users/dto/create-user.dto.ts | 4 ++++ 8 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 src/auth/dto/login.dto.ts create mode 100644 src/auth/dto/login.response.dto.ts create mode 100644 src/auth/dto/register.response.dto.ts diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..30bc162 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +/node_modules \ No newline at end of file diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 1091e40..dd11c35 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -15,18 +15,33 @@ import { LocalAuthGuard } from './guards/local-auth.guard'; import JwtAuthGuard from './guards/jwt-auth.guard'; import { RequestWithUser } from './interfaces/request-with-user.interface'; +import { RegisterResponseDto } from './dto/register.response.dto'; +import { + ApiBody, + ApiCreatedResponse, + ApiOkResponse, + ApiTags, +} from '@nestjs/swagger'; +import { LoginDto } from './dto/login.dto'; +import { LoginResponseDto } from './dto/login.response.dto'; +@ApiTags('auth') @Controller('auth') export class AuthController { constructor(private readonly authService: AuthService) {} @Post('register') - async register(@Body() registerData: RegisterDto) { + @ApiCreatedResponse({ type: RegisterResponseDto }) + async register( + @Body() registerData: RegisterDto, + ): Promise { return this.authService.register(registerData); } @HttpCode(200) @UseGuards(LocalAuthGuard) + @ApiBody({ type: LoginDto }) + @ApiOkResponse({ type: LoginResponseDto }) @Post('login') async login(@Req() request: RequestWithUser, @Res() response: Response) { const user = request.user; @@ -38,8 +53,9 @@ export class AuthController { response.json({ user: omit(user, 'password') }); } + @HttpCode(200) @UseGuards(JwtAuthGuard) - @Post('log-out') + @Post('logout') async logOut(@Req() request: RequestWithUser, @Res() response: Response) { response.setHeader('Set-Cookie', this.authService.getCookieForLogOut()); return response.sendStatus(200); diff --git a/src/auth/dto/login.dto.ts b/src/auth/dto/login.dto.ts new file mode 100644 index 0000000..4277458 --- /dev/null +++ b/src/auth/dto/login.dto.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator'; + +export class LoginDto { + @ApiProperty({ minLength: 5 }) + @IsNotEmpty() + @IsString() + @MinLength(5) + password: string; + + @ApiProperty() + @IsEmail() + email: string; +} diff --git a/src/auth/dto/login.response.dto.ts b/src/auth/dto/login.response.dto.ts new file mode 100644 index 0000000..4cfe4aa --- /dev/null +++ b/src/auth/dto/login.response.dto.ts @@ -0,0 +1,3 @@ +import { RegisterResponseDto } from './register.response.dto'; + +export class LoginResponseDto extends RegisterResponseDto {} diff --git a/src/auth/dto/register.response.dto.ts b/src/auth/dto/register.response.dto.ts new file mode 100644 index 0000000..b8a8ad1 --- /dev/null +++ b/src/auth/dto/register.response.dto.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class RegisterResponseDto { + @ApiProperty() + id: number; + + @ApiProperty() + name: string; + + @ApiProperty() + email: string; +} diff --git a/src/auth/strategies/local.strategy.ts b/src/auth/strategies/local.strategy.ts index bd9a696..63eeb83 100644 --- a/src/auth/strategies/local.strategy.ts +++ b/src/auth/strategies/local.strategy.ts @@ -6,7 +6,7 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class LocalStrategy extends PassportStrategy(Strategy) { - constructor(private authenticationService: AuthService) { + constructor(private authService: AuthService) { super({ usernameField: 'email', }); @@ -15,6 +15,6 @@ export class LocalStrategy extends PassportStrategy(Strategy) { email: string, password: string, ): Promise> { - return this.authenticationService.getAuthenticatedUser(email, password); + return this.authService.getAuthenticatedUser(email, password); } } diff --git a/src/main.ts b/src/main.ts index 423f9c9..b025201 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,9 @@ import helmet from 'helmet'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import { config } from 'dotenv'; + +config(); async function bootstrap() { const app = await NestFactory.create(AppModule); @@ -17,11 +20,10 @@ async function bootstrap() { .setTitle('Dating') .setDescription('Dating app API description') .setVersion('1.0') - .addTag('dating') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('docs', app, document); - await app.listen(process.env.PORT); + await app.listen(process.env.API_PORT); } bootstrap(); diff --git a/src/users/dto/create-user.dto.ts b/src/users/dto/create-user.dto.ts index 5a74ee2..60e5163 100644 --- a/src/users/dto/create-user.dto.ts +++ b/src/users/dto/create-user.dto.ts @@ -1,15 +1,19 @@ +import { ApiProperty } from '@nestjs/swagger'; import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator'; export class CreateUserDto { + @ApiProperty() @IsNotEmpty() @IsString() name: string; + @ApiProperty({ minLength: 5 }) @IsNotEmpty() @IsString() @MinLength(5) password: string; + @ApiProperty() @IsEmail() email: string; }