完成me接口
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ export class UserService {
|
||||
|
||||
async findOne(options: UserFindOptions | UserFindOptions[]): Promise<User | null> {
|
||||
if (Object.keys(options).length === 0) {
|
||||
return null;
|
||||
throw new BadRequestException('查询条件不能为空');
|
||||
}
|
||||
return this.userRepository.findOne({ where: options });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user