微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 nestJs 中使用护照本地策略对多个实体进行身份验证?

如何解决如何在 nestJs 中使用护照本地策略对多个实体进行身份验证?

我有两个不同的实体:公司和用户都必须使用电子邮件和密码进行身份验证。 我设置了 2 个不同的类扩展护照策略如下:

@Injectable()
export class LocalStrategyCompany extends PassportStrategy(Strategy) {
  constructor(private companyAuthenticationService: CompanyAuthenticationService) {
    super({
      usernameField: 'email'
    });
  }
  async validate(email: string,password: string): Promise<Company> {
    const company = this.companyAuthenticationService.getAuthenticatedCompany(email,password);
    if (!company) {
      throw new UnauthorizedException();
    }
    return company;
  }
  }

和本地认证保护类:

@Injectable()
export class LocalAuthenticationGuard extends AuthGuard('local') {}

和 auth 控制器中的这个登录方法

@HttpCode(200)
  @UseGuards(LocalAuthenticationGuard)
  @Post('log-in')
  async logIn(@Req() request: RequestWithUser) {
    const {user} = request;
    const cookie = this.authenticationService.getCookieWithJwtToken(user.id);
    request.res.setHeader('Set-Cookie',cookie);
    return user;
  }

我在 CompanyAuthentication 模块中创建了相同的模块,但是当我尝试使用公司登录时,我收到错误的凭据错误。你能帮忙吗?顺便说一句,我正在关注本教程:https://wanago.io/2020/05/25/api-nestjs-authenticating-users-bcrypt-passport-jwt-cookies/

解决方法

我认为 fig = plt.figure(constrained_layout=True) gs = fig.add_gridspec(18,9) ax = fig.add_subplot(gs[3:6,1:3],zorder=1) #this is the target subplot ax = draw_football_field(ax,other_args) # modify axes instance from 'this' fig 会返回一个 Promise,因此您需要使用 this.companyAuthenticationService.getAuthenticatedCompany 关键字。

await

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?