auth swagger spec
This commit is contained in:
parent
968d83e58b
commit
bf38ec5355
|
@ -0,0 +1 @@
|
||||||
|
/node_modules
|
|
@ -15,18 +15,33 @@ import { LocalAuthGuard } from './guards/local-auth.guard';
|
||||||
|
|
||||||
import JwtAuthGuard from './guards/jwt-auth.guard';
|
import JwtAuthGuard from './guards/jwt-auth.guard';
|
||||||
import { RequestWithUser } from './interfaces/request-with-user.interface';
|
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')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) {}
|
constructor(private readonly authService: AuthService) {}
|
||||||
|
|
||||||
@Post('register')
|
@Post('register')
|
||||||
async register(@Body() registerData: RegisterDto) {
|
@ApiCreatedResponse({ type: RegisterResponseDto })
|
||||||
|
async register(
|
||||||
|
@Body() registerData: RegisterDto,
|
||||||
|
): Promise<RegisterResponseDto> {
|
||||||
return this.authService.register(registerData);
|
return this.authService.register(registerData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(200)
|
@HttpCode(200)
|
||||||
@UseGuards(LocalAuthGuard)
|
@UseGuards(LocalAuthGuard)
|
||||||
|
@ApiBody({ type: LoginDto })
|
||||||
|
@ApiOkResponse({ type: LoginResponseDto })
|
||||||
@Post('login')
|
@Post('login')
|
||||||
async login(@Req() request: RequestWithUser, @Res() response: Response) {
|
async login(@Req() request: RequestWithUser, @Res() response: Response) {
|
||||||
const user = request.user;
|
const user = request.user;
|
||||||
|
@ -38,8 +53,9 @@ export class AuthController {
|
||||||
response.json({ user: omit(user, 'password') });
|
response.json({ user: omit(user, 'password') });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@HttpCode(200)
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Post('log-out')
|
@Post('logout')
|
||||||
async logOut(@Req() request: RequestWithUser, @Res() response: Response) {
|
async logOut(@Req() request: RequestWithUser, @Res() response: Response) {
|
||||||
response.setHeader('Set-Cookie', this.authService.getCookieForLogOut());
|
response.setHeader('Set-Cookie', this.authService.getCookieForLogOut());
|
||||||
return response.sendStatus(200);
|
return response.sendStatus(200);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
import { RegisterResponseDto } from './register.response.dto';
|
||||||
|
|
||||||
|
export class LoginResponseDto extends RegisterResponseDto {}
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
|
||||||
|
export class RegisterResponseDto {
|
||||||
|
@ApiProperty()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
email: string;
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LocalStrategy extends PassportStrategy(Strategy) {
|
export class LocalStrategy extends PassportStrategy(Strategy) {
|
||||||
constructor(private authenticationService: AuthService) {
|
constructor(private authService: AuthService) {
|
||||||
super({
|
super({
|
||||||
usernameField: 'email',
|
usernameField: 'email',
|
||||||
});
|
});
|
||||||
|
@ -15,6 +15,6 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
|
||||||
email: string,
|
email: string,
|
||||||
password: string,
|
password: string,
|
||||||
): Promise<Omit<User, 'password'>> {
|
): Promise<Omit<User, 'password'>> {
|
||||||
return this.authenticationService.getAuthenticatedUser(email, password);
|
return this.authService.getAuthenticatedUser(email, password);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ import helmet from 'helmet';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { ValidationPipe } from '@nestjs/common';
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||||
|
import { config } from 'dotenv';
|
||||||
|
|
||||||
|
config();
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
|
@ -17,11 +20,10 @@ async function bootstrap() {
|
||||||
.setTitle('Dating')
|
.setTitle('Dating')
|
||||||
.setDescription('Dating app API description')
|
.setDescription('Dating app API description')
|
||||||
.setVersion('1.0')
|
.setVersion('1.0')
|
||||||
.addTag('dating')
|
|
||||||
.build();
|
.build();
|
||||||
const document = SwaggerModule.createDocument(app, config);
|
const document = SwaggerModule.createDocument(app, config);
|
||||||
SwaggerModule.setup('docs', app, document);
|
SwaggerModule.setup('docs', app, document);
|
||||||
|
|
||||||
await app.listen(process.env.PORT);
|
await app.listen(process.env.API_PORT);
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
|
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
|
||||||
|
|
||||||
export class CreateUserDto {
|
export class CreateUserDto {
|
||||||
|
@ApiProperty()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@ApiProperty({ minLength: 5 })
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@MinLength(5)
|
@MinLength(5)
|
||||||
password: string;
|
password: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
@IsEmail()
|
@IsEmail()
|
||||||
email: string;
|
email: string;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue