diff --git a/tone-page-server/src/user/user.controller.ts b/tone-page-server/src/user/user.controller.ts index 57b5824..4a14c4a 100644 --- a/tone-page-server/src/user/user.controller.ts +++ b/tone-page-server/src/user/user.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Injectable, UseGuards } from '@nestjs/common'; +import { Controller, Get, Injectable, Request, UnauthorizedException, UseGuards } from '@nestjs/common'; import { UserService } from './user.service'; import { AuthGuard } from '@nestjs/passport'; @@ -11,7 +11,11 @@ export class UserController { @UseGuards(AuthGuard('jwt')) @Get('me') - async getMe() { - return 'ok'; + async getMe(@Request() req) { + const { user } = req; + if (!user || !user.userId) { + throw new UnauthorizedException('Unauthorized'); + } + return this.userService.findOne({ userId: user.userId }); } } diff --git a/tone-page-server/src/user/user.service.ts b/tone-page-server/src/user/user.service.ts index af804a8..07a21fd 100644 --- a/tone-page-server/src/user/user.service.ts +++ b/tone-page-server/src/user/user.service.ts @@ -16,7 +16,7 @@ export class UserService { async findOne(options: UserFindOptions | UserFindOptions[]): Promise { if (Object.keys(options).length === 0) { - return null; + throw new BadRequestException('查询条件不能为空'); } return this.userRepository.findOne({ where: options }); }