Skip to content

Commit

Permalink
feat: Update swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Smirnov committed Sep 17, 2023
1 parent b033e7c commit 7e9eaeb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
24 changes: 21 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { AuthService } from '@/auth/auth.service';
import { LoginDto } from '@/auth/dto/login.dto';
import { CreateUserDto } from '@/user/dto/create-user.dto';
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger';
import {
Body,
Controller,
HttpCode,
NotFoundException,
Post,
UnauthorizedException,
} from '@nestjs/common';
import {
ApiBody,
ApiNotFoundResponse,
ApiOperation,
ApiResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';

@ApiTags('Auth')
@Controller('auth')
Expand All @@ -10,7 +25,10 @@ export class AuthController {

@ApiOperation({ summary: 'Login user' })
@ApiBody({ type: CreateUserDto })
@HttpCode(201)
@ApiResponse({ type: LoginDto, status: 200 })
@ApiNotFoundResponse({ type: NotFoundException })
@ApiUnauthorizedResponse({ type: UnauthorizedException })
@HttpCode(200)
@Post('login')
login(@Body() dto: CreateUserDto) {
return this.authService.login(dto);
Expand Down
6 changes: 2 additions & 4 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoginDto } from '@/auth/dto/login.dto';
import { CreateUserDto } from '@/user/dto/create-user.dto';
import { User } from '@/user/entities/user.entity';
import { UserService } from '@/user/user.service';
Expand Down Expand Up @@ -49,9 +50,6 @@ export class AuthService {
const accessToken = this.jwtService.sign(payload);
const verify = this.jwtService.verify(accessToken);

return {
accessToken,
expires: verify.exp,
};
return new LoginDto(accessToken, verify.exp);
}
}
17 changes: 17 additions & 0 deletions src/auth/dto/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber, IsString } from 'class-validator';

export class LoginDto {
constructor(accessToken: string, expires: number) {
this.accessToken = accessToken;
this.expires = expires;
}

@ApiProperty({ example: 'eyJhbGciOiJIUzUxMiIsI...' })
@IsString()
accessToken: string;

@ApiProperty({ example: Math.round(Date.now() / 1000) })
@IsNumber()
expires: number;
}
3 changes: 2 additions & 1 deletion src/child/child.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Param,
Patch,
Post,
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import {
Expand All @@ -30,7 +31,7 @@ import {

@ApiTags('Child')
@ApiBearerAuth()
@ApiUnauthorizedResponse()
@ApiUnauthorizedResponse({ type: UnauthorizedException })
@UseGuards(AuthGuard)
@Controller('child')
export class ChildController {
Expand Down
4 changes: 2 additions & 2 deletions src/client/client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Param,
Patch,
Post,
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import {
Expand All @@ -26,11 +27,10 @@ import { Client } from '@/client/client';
import { ClientService } from '@/client/client.service';
import { CreateClientDto } from '@/client/dto/createClientDto';
import { UpdateClientDto } from '@/client/dto/updateClientDto';
import { Request } from 'express';

@ApiTags('Client')
@ApiBearerAuth()
@ApiUnauthorizedResponse()
@ApiUnauthorizedResponse({ type: UnauthorizedException })
@UseGuards(AuthGuard)
@Controller('client')
export class ClientController {
Expand Down
3 changes: 2 additions & 1 deletion src/teacher/teacher.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
HttpCode,
NotFoundException,
UseGuards,
UnauthorizedException,
} from '@nestjs/common';
import {
ApiBearerAuth,
Expand All @@ -29,7 +30,7 @@ import { UpdateTeacherDto } from '@/teacher/dto/update-teacher.dto';

@ApiTags('Teacher')
@ApiBearerAuth()
@ApiUnauthorizedResponse()
@ApiUnauthorizedResponse({ type: UnauthorizedException })
@UseGuards(AuthGuard)
@Controller('teacher')
export class TeacherController {
Expand Down

0 comments on commit 7e9eaeb

Please sign in to comment.