feat: 添加VO类型的文档生成
parent
1819a1a8df
commit
6bdc589a6f
|
|
@ -7,6 +7,7 @@
|
|||
{
|
||||
"name": "@nestjs/swagger",
|
||||
"options": {
|
||||
"dtoFileNameSuffix": [".vo.ts", ".dto.ts", ".entity.ts"],
|
||||
"introspectComments": true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ export function addResponseWrapper(doc: OpenAPIObject) {
|
|||
properties: {
|
||||
data: schema,
|
||||
},
|
||||
required: ['data'],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,21 +1,22 @@
|
|||
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthUserDto } from './dto/auth-user.dto';
|
||||
import { Public } from './jwt';
|
||||
import { LoginedUserVo } from './vo/logined-user.vo';
|
||||
|
||||
@ApiTags('auth')
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
@Public()
|
||||
/**
|
||||
* 账号登陆
|
||||
*/
|
||||
@Post('login')
|
||||
@Public()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiResponse({ status: HttpStatus.UNAUTHORIZED, description: '账号或密码错误' })
|
||||
@ApiResponse({ description: '登录成功' })
|
||||
@ApiOperation({ description: '账号登录', operationId: 'login' })
|
||||
login(@Body() user: AuthUserDto) {
|
||||
login(@Body() user: AuthUserDto): Promise<LoginedUserVo> {
|
||||
return this.authService.signIn(user);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|||
import { JwtService } from '@nestjs/jwt';
|
||||
import { UserService } from '../user';
|
||||
import { AuthUserDto } from './dto/auth-user.dto';
|
||||
import { LoginedUserVo } from './vo/logined-user.vo';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
|
|
@ -9,13 +10,17 @@ export class AuthService {
|
|||
|
||||
async signIn(authUserDto: AuthUserDto) {
|
||||
const user = await this.userService.findByUsername(authUserDto.username);
|
||||
const { password, ...result } = user;
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('用户名不存在');
|
||||
}
|
||||
if (password !== authUserDto.password) {
|
||||
if (user.password !== authUserDto.password) {
|
||||
throw new UnauthorizedException('密码错误');
|
||||
}
|
||||
return this.jwtService.signAsync(result);
|
||||
const loginedUser = new LoginedUserVo();
|
||||
for (const key in loginedUser) {
|
||||
loginedUser[key] = user[key];
|
||||
}
|
||||
loginedUser.token = await this.jwtService.signAsync({ id: user.id, username: user.username });
|
||||
return loginedUser;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
import { User } from '@/modules/user';
|
||||
import { OmitType } from '@nestjs/swagger';
|
||||
|
||||
export class LoginedUserVo extends OmitType(User, ['password'] as const) {
|
||||
/**
|
||||
* 访问令牌
|
||||
* @example 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MjIsInVzZXJuYW1lIjoianVldGFuIiwiaWF0IjoxNjkxMTM5MjI3LCJleHAiOjE2OTExOTkyMjd9.6z7f-xfsHABbsyg401o2boKeqNQ1epPDYfEdavIcfYc'
|
||||
*/
|
||||
token: string;
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ export class UserService extends BaseService {
|
|||
* 根据用户名查找用户
|
||||
*/
|
||||
findByUsername(username: string) {
|
||||
return this.userRepository.findOneOrFail({ where: { username } });
|
||||
return this.userRepository.findOne({ where: { username } });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue