【问题标题】:how to execute UseGuard in nestjs microservie如何在nestjs微服务中执行Use Guard
【发布时间】:2021-02-25 12:16:03
【问题描述】:

我打算将我的身份验证部分用作微服务,因此我用户嵌套了 js TCP 传输器,但我不知道如何在我的微服务中执行护照本地策略,我使用了以下代码

    @MessagePattern('login')
    @UseGuards(LocalAuthGuard)
    localLogin( loginDto: LoginDto) {
        console.log('awa')
        return loginDto
       // return this.authService.localLogin(req.user, loginDto.email);
    }

但它不知道如何在微服务中授权本地策略我的本地策略如下所示

import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
import { Request } from 'express';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
   
    constructor(private authService: AuthService) {
        super({
            usernameField: 'email',
            passwordField: 'password',
            passReqToCallback: true,
        });
    }

    async validate(
        email: string,
        password: string,
    ): Promise<{ id: string; isVerified: boolean }> {
        try {
            const user = await this.authService.validateLocalUser(email, password);
            if (!user) {
                //throw new UnauthorizedException();
            }
            return { id: user.id,isVerified: user.isVerified };
        } catch (error) {
            console.log('error')
        }
       
    }
}

这是我得到的错误

node:71975) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'reply' of null
    at AppExceptionFilter.catch (/Users/imanthaatapattu/apps/beta-identity-service/node_modules/@nestjs/core/exceptions/base-exception-filter.js:26:24)
    at AppExceptionFilter.catch (/Users/imanthaatapattu/apps/beta-identity-service/dist/common/exception-filters/app-exception.filter.js:29:20)
    at RpcExceptionsHandler.invokeCustomFilters (/Users/imanthaatapattu/apps/beta-identity-service/node_modules/@nestjs/microservices/exceptions/rpc-exceptions-handler.js:34:32)
    at RpcExceptionsHandler.handle (/Users/imanthaatapattu/apps/beta-identity-service/node_modules/@nestjs/microservices/exceptions/rpc-exceptions-handler.js:13:36)
    at RpcProxy.handleError (/Users/imanthaatapattu/apps/beta-identity-service/node_modules/@nestjs/microservices/context/rpc-proxy.js:24:34)
    at /Users/imanthaatapattu/apps/beta-identity-service/node_modules/@nestjs/microservices/context/rpc-proxy.js:17:29
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:71975) 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(). (rejection id: 3)
(node:71975) [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.

【问题讨论】:

  • 'TypeError: Cannot read property 'reply' of null',你能告诉我们你使用reply的代码吗
  • @Youba 我没有在任何地方使用回复

标签: api microservices nestjs


【解决方案1】:

Passport 是一个 Express 中间件,旨在通过 HTTP 使用。您正在将它与中间件一起使用,这并不完全是预期的。如果您想继续将护照与微服务一起使用,您需要提供它在 HTTP 上下文中所期望的相同值。你可以看到Nest's implementation of the AuthGuard here。其中最重要的部分是确保getRequest 返回一个具有body 属性和body 具有emailpassword 属性的对象。编写自己的微服务特定保护程序很可能会更容易,它可以确定请求是否有效(通过 JWT 或类似方法)

【讨论】:

  • 抱歉,我是 Nestjs 新手,创建我自己的微服务特定守卫是什么意思?
  • 我的意思是创建自己的警卫,不使用护照,进行身份验证检查。您可能需要使用context.switchToRpc() 来确保您在警卫中获得正确的信息。
猜你喜欢
  • 2019-07-01
  • 2021-01-12
  • 2019-06-14
  • 2018-07-28
  • 1970-01-01
  • 2020-02-04
  • 2020-02-10
  • 2023-02-05
  • 2019-05-28
相关资源
最近更新 更多