完成me接口

This commit is contained in:
2025-05-13 10:53:25 +08:00
parent 134335021d
commit 5e7ed3d6f7
2 changed files with 8 additions and 4 deletions

View File

@@ -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 });
}
}

View File

@@ -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 });
}