如何在nestJs中使用passport本地策略对多个实体进行身份验证?
我有两个不同的实体:公司和用户都必须通过电子邮件和密码进行身份验证。 我设置了两个不同的类扩展passport策略,如下所示:如何在nestJs中使用passport本地策略对多个实体进行身份验证?,nestjs,nestjs-passport,Nestjs,Nestjs Passport,我有两个不同的实体:公司和用户都必须通过电子邮件和密码进行身份验证。 我设置了两个不同的类扩展passport策略,如下所示: @Injectable() export class LocalStrategyCompany extends PassportStrategy(Strategy) { constructor(private companyAuthenticationService: CompanyAuthenticationService) { super({
@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;
}
}
以及身份验证控制器中的此登录方法:
@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模块中创建了相同的身份验证,但当我尝试登录公司时,我收到错误的凭据错误。你能帮忙吗?顺便说一句,我遵循本教程:我认为
this.companyAuthenticationService.getAuthenticatedCompany
返回一个承诺,因此需要使用wait
关键字
const company=wait this.companyAuthenticationService.GetAuthenticationCompany(电子邮件、密码);
感谢您的回复,但这并不能解决我的问题。
@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;
}