diff --git a/nest-cli.json b/nest-cli.json index 2c97420..2656a7e 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -7,6 +7,7 @@ { "name": "@nestjs/swagger", "options": { + "dtoFileNameSuffix": [".vo.ts", ".dto.ts", ".entity.ts"], "introspectComments": true } } diff --git a/src/common/swagger/util.ts b/src/common/swagger/util.ts index 953cc98..6fd2990 100644 --- a/src/common/swagger/util.ts +++ b/src/common/swagger/util.ts @@ -47,6 +47,7 @@ export function addResponseWrapper(doc: OpenAPIObject) { properties: { data: schema, }, + required: ['data'], }, ], }; diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 2d6c663..a392ba8 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -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 { return this.authService.signIn(user); } } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 2b10488..abbd665 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -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; } } diff --git a/src/modules/auth/vo/logined-user.vo.ts b/src/modules/auth/vo/logined-user.vo.ts new file mode 100644 index 0000000..48fd3c8 --- /dev/null +++ b/src/modules/auth/vo/logined-user.vo.ts @@ -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; +} diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index aab8d10..9c37696 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -61,7 +61,7 @@ export class UserService extends BaseService { * 根据用户名查找用户 */ findByUsername(username: string) { - return this.userRepository.findOneOrFail({ where: { username } }); + return this.userRepository.findOne({ where: { username } }); } /**