完成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 { UserService } from './user.service';
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
|
|
||||||
@@ -11,7 +11,11 @@ export class UserController {
|
|||||||
|
|
||||||
@UseGuards(AuthGuard('jwt'))
|
@UseGuards(AuthGuard('jwt'))
|
||||||
@Get('me')
|
@Get('me')
|
||||||
async getMe() {
|
async getMe(@Request() req) {
|
||||||
return 'ok';
|
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> {
|
async findOne(options: UserFindOptions | UserFindOptions[]): Promise<User | null> {
|
||||||
if (Object.keys(options).length === 0) {
|
if (Object.keys(options).length === 0) {
|
||||||
return null;
|
throw new BadRequestException('查询条件不能为空');
|
||||||
}
|
}
|
||||||
return this.userRepository.findOne({ where: options });
|
return this.userRepository.findOne({ where: options });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user