0

私は 2 つの異なるエンティティを持っています。会社とユーザーは、どちらも電子メールとパスワードで認証する必要があります。次のように、パスポート戦略を拡張する 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') {}

そして、認証コントローラーのこのログイン方法:

@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/

4

1 に答える 1