nestjs扩展了jwt保护

nestjs扩展了jwt保护,nestjs,nestjs-jwt,Nestjs,Nestjs Jwt,为了检查用户表中是否存在用户,我扩展了jwt guard。以下是我的代码: import { ExecutionContext, Injectable, UnauthorizedException, } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { error } from 'console'; import { UsersService } from 'src/users/

为了检查用户表中是否存在用户,我扩展了jwt guard。以下是我的代码:

import {
  ExecutionContext,
  Injectable,
  UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { error } from 'console';
import { UsersService } from 'src/users/users.service';
import { Role } from './role.enum';
@Injectable()
export class JwtUserGuard extends AuthGuard('jwt') {
  constructor(private readonly userService: UsersService) {
    super();
  }
  canActivate(context: ExecutionContext) {
    return super.canActivate(context);
  }

  handleRequest(err, user, info) {
    this.userService.findByEmail(user.email).then((user) => {
  if (user === undefined) {
    throw new UnauthorizedException();
  }
  return user;
}).catch(error=>{
  throw new UnauthorizedException();
});

    if (user.role !== Role.User) {
      throw new UnauthorizedException();
    }
    return user;
  }
}
但我总是会出错

(node:4504) UnhandledPromiseRejectionWarning: Error: Unauthorized
    at /media/ridwan/storage/workspace/backend/javascript/nestjs/queueing/dist/auth/jwt-user.guard.js:28:23
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4504) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4504) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我的问题是如何处理未处理的PromisejectionWarning即使用户不存在,我的代码仍在运行?提前感谢。

使用
PassportStrategy
混合并将
findByEmail
逻辑移动到正确的位置。他们在这里解释了如何做到这一点:

您正在混合使用同步和异步编程方法,方法是使用承诺(使用链接的
,然后是
catch
),并且一开始就不返回承诺。我相信Nest的
handleRequest
方法不允许异步方法。因此,所发生的情况是,您正在启动一个异步进程(承诺调用
this.userService.findByEmail
)并抛出一个错误,但您正在(同步)返回
user
属性(或抛出一个正确处理的不同错误)。然后,当承诺解决(拒绝)时,您有一个未处理的
抛出
,意思是
未处理的PromiserRejection


我不明白为什么您不能在策略文件中执行所有这些逻辑,因为
handleRequest
是在首先调用
validate
之后执行的。

感谢您的回复,但我需要根据该令牌的信息添加一些额外的逻辑,例如,如果该令牌属于用户或管理员,则确定访问策略,因为我使用不同的表对每个用户进行身份验证。对于您的答案,我想做的是为每个用户创建不同的jwt防护,如
JwtUserGuard
JwtAdminGuard
JwtTenantGuard
。因为用户使用电子邮件登录,而其他用户使用用户名,所以我必须检查应该从令牌中使用哪个表。为什么您不能将其作为
JwtStrategy
的一部分,并将逻辑整合到其中?我不明白为什么你需要引入多个守卫我不知道如何只为特定用户创建特定的路由,比如只为管理员创建用户列表的路由可以访问它,这就是为什么我创建了不同类型的守卫,你能给我举一些在
JwtStrategy
中如何做的例子吗?总的来说,你似乎试图将
RolesGuard
JwtGuard
合并成一个单独的守卫。我建议做的是使用
JwtGuard
来断言传递的jwt的有效性,并使用
RolesGuard
来验证呼叫用户是否有权访问路由。通过这种方式,您可以分离关注点,而不必担心将逻辑与我试图创建的单个保护混淆,因为我不知道如何将用户对象从令牌传递和读取到
RolesGuard
。使用`const{user}=context.switchToHttp().getRequest();`它给了我一个未定义的结果,也许你们可以给我一个例子,如何从roleguard中的令牌传递和读取用户对象?