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') {}
以及身份验证控制器中的此登录方法:
@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/
解决方案
我认为这会this.companyAuthenticationService.getAuthenticatedCompany
返回一个 Promise,因此您需要使用await
关键字。
const company = await this.companyAuthenticationService.getAuthenticatedCompany(email, password);
推荐阅读
- java - 元素未添加到 ArrayList Java - Android
- python - 将数据框插入具有不同列名的数据库表
- php - yii2 bootstrap 4 没有在 ajax 上突出显示错误
- javascript - Chrome 扩展缺少 Set-Cookie 标头
- amazon-rds - 带有 AWS RDS 的 Spring Cloud AWS
- python-3.x - 我正在尝试在 python 上制作一个票价计算器,此代码部分在打印部分之后不运行
- android - 如何在 Azure 构建管道中忽略 Android 构建变体?
- python - 将列的值与变量进行比较并创建新列
- android - 将主题设置为片段
- git - 开发人员提交时如何在我的服务器中触发