auth swagger spec

This commit is contained in:
Pravdin Egor 2023-09-19 21:17:00 +07:00
parent 968d83e58b
commit bf38ec5355
8 changed files with 58 additions and 6 deletions

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
/node_modules

View File

@ -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<RegisterResponseDto> {
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);

14
src/auth/dto/login.dto.ts Normal file
View File

@ -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;
}

View File

@ -0,0 +1,3 @@
import { RegisterResponseDto } from './register.response.dto';
export class LoginResponseDto extends RegisterResponseDto {}

View File

@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
export class RegisterResponseDto {
@ApiProperty()
id: number;
@ApiProperty()
name: string;
@ApiProperty()
email: string;
}

View File

@ -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<Omit<User, 'password'>> {
return this.authenticationService.getAuthenticatedUser(email, password);
return this.authService.getAuthenticatedUser(email, password);
}
}

View File

@ -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();

View File

@ -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;
}