Add a swagger (#79)
This commit is contained in:
@@ -1,53 +1,71 @@
|
||||
import { Controller, Request, Post, Get, UseGuards, Res, Body, Delete } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Request,
|
||||
Post,
|
||||
Get,
|
||||
UseGuards,
|
||||
Body,
|
||||
Delete,
|
||||
BadRequestException,
|
||||
HttpCode,
|
||||
} from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { JwtAuthGuard } from './jwt-auth.guard';
|
||||
import { LocalAuthGuard } from './local-auth.guard';
|
||||
import { RegisterDto } from './dto/register.dto';
|
||||
import { Response } from 'express';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { ApiBearerAuth, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import {
|
||||
ApiBearerAuth,
|
||||
ApiBody,
|
||||
ApiOkResponse,
|
||||
ApiParam,
|
||||
ApiTags,
|
||||
ApiUnauthorizedResponse,
|
||||
} from '@nestjs/swagger';
|
||||
import { User } from '../models/user';
|
||||
import { JwtToken } from './models/jwt';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
|
||||
@ApiTags('auth')
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(
|
||||
private authService: AuthService,
|
||||
private usersService: UsersService,
|
||||
private configService: ConfigService
|
||||
) {}
|
||||
|
||||
@Post('register')
|
||||
async register(@Body() registerDto: RegisterDto, @Res() res: Response) {
|
||||
async register(@Body() registerDto: RegisterDto): Promise<void> {
|
||||
try {
|
||||
await this.usersService.createUser(registerDto);
|
||||
return res.status(200).json({"status": "user created"});
|
||||
} catch {
|
||||
return res.status(400).json({"status": "user not created"});
|
||||
throw new BadRequestException();
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBody({ type: LoginDto })
|
||||
@HttpCode(200)
|
||||
@UseGuards(LocalAuthGuard)
|
||||
@Post('login')
|
||||
async login(@Request() req) {
|
||||
async login(@Request() req: any): Promise<JwtToken> {
|
||||
return this.authService.login(req.user);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ description: 'Successfully logged in' })
|
||||
@ApiOkResponse({ description: 'Successfully logged in', type: User })
|
||||
@ApiUnauthorizedResponse({ description: 'Invalid token' })
|
||||
@Get('me')
|
||||
getProfile(@Request() req) {
|
||||
return req.user;
|
||||
}
|
||||
getProfile(@Request() req: any): User {
|
||||
return req.user;
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ description: 'Successfully deleted' })
|
||||
@ApiOkResponse({ description: 'Successfully deleted', type: User})
|
||||
@ApiUnauthorizedResponse({ description: 'Invalid token' })
|
||||
@Delete('me')
|
||||
deleteSelf(@Request() req) {
|
||||
return this.usersService.deleteUser({"id": req.user.id})
|
||||
}
|
||||
|
||||
deleteSelf(@Request() req: any): Promise<User> {
|
||||
return this.usersService.deleteUser({ id: req.user.id });
|
||||
}
|
||||
}
|
||||
|
||||
6
back/src/auth/models/jwt.ts
Normal file
6
back/src/auth/models/jwt.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class JwtToken {
|
||||
@ApiProperty()
|
||||
access_token: string;
|
||||
}
|
||||
12
back/src/models/user.ts
Normal file
12
back/src/models/user.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class User {
|
||||
@ApiProperty()
|
||||
id: number;
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
@ApiProperty()
|
||||
password: string;
|
||||
@ApiProperty()
|
||||
email: string;
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class CreateUserDto {
|
||||
email: string;
|
||||
username: string;
|
||||
password: string;
|
||||
@ApiProperty()
|
||||
email: string;
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
@ApiProperty()
|
||||
password: string;
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export class User {}
|
||||
@@ -1,34 +1,55 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Put } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { ApiNotFoundResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { User } from 'src/models/user';
|
||||
|
||||
@ApiTags('users')
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createUserDto: CreateUserDto) {
|
||||
return this.usersService.createUser(createUserDto);
|
||||
}
|
||||
@Post()
|
||||
create(@Body() createUserDto: CreateUserDto): Promise<User> {
|
||||
return this.usersService.createUser(createUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.usersService.users({});
|
||||
}
|
||||
@Get()
|
||||
findAll(): Promise<User[]> {
|
||||
return this.usersService.users({});
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: number) {
|
||||
return this.usersService.user({"id": +id});
|
||||
}
|
||||
@Get(':id')
|
||||
@ApiNotFoundResponse()
|
||||
async findOne(@Param('id') id: number): Promise<User> {
|
||||
const ret = await this.usersService.user({ id: +id });
|
||||
if (!ret) throw new NotFoundException();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
|
||||
return this.usersService.updateUser({where: {"id": +id}, data: updateUserDto});
|
||||
}
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Body() updateUserDto: UpdateUserDto,
|
||||
): Promise<User> {
|
||||
return this.usersService.updateUser({
|
||||
where: { id: +id },
|
||||
data: updateUserDto,
|
||||
});
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.usersService.deleteUser({"id": +id});
|
||||
}
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string): Promise<User> {
|
||||
return this.usersService.deleteUser({ id: +id });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ Login
|
||||
[Arguments] ${username}
|
||||
&{res}= POST /auth/login {"username": "${username}", "password": "password-${username}"}
|
||||
Output
|
||||
Integer response status 201
|
||||
Integer response status 200
|
||||
String response body access_token
|
||||
Set Headers {"Authorization": "Bearer ${res.body.access_token}"}
|
||||
|
||||
@@ -21,7 +21,7 @@ Register
|
||||
... /auth/register
|
||||
... {"username": "${username}", "password": "password-${username}", "email": "${username}@chromacase.moe"}
|
||||
Output
|
||||
Integer response status 200
|
||||
Integer response status 201
|
||||
|
||||
Logout
|
||||
[Documentation] Logout the current user, only the local client is affected.
|
||||
|
||||
Reference in New Issue
Block a user