我想用电话号码登录,但我在 Nestjs 上没有得到电话号码。
所以我在构造函数中添加了 usernameField: 'email'、phoneField: 'phoneNumber',用于检查,但它不起作用。
在 webstorm 中我写了 console.log(
pass ${password}
) 我看到:
本地策略:
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({
usernameField: 'email',
phoneField: 'phoneNumber',
});
}
async validate(
email: string,
password: string,
phoneNumber: string,
): Promise<any> {
console.log(` mail ${email}`)
console.log(` pass ${password}`)
console.log(` phone ${phoneNumber}`)
const user = await this.authService.validateUser(
email,
password,
phoneNumber,
);
// console.log(user)
if (!user) {
throw new UnauthorizedException('Неверный логин или пароль');
}
return user;
}
}
我已经做到了。
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(
private authService: AuthService,
private moduleRef: ModuleRef
) {
super({
passReqToCallback: true,
usernameField: 'email',
phoneNumber: 'phoneNumber',
passwordField: 'password',
});
}
async validate(
request: Request,
email: string,
phoneNumber: string,
password: string,
): Promise<any> {
const contextId = ContextIdFactory.getByRequest(request);
const flex = await this.moduleRef.resolve(AuthService, contextId);
const user = await this.authService.validateUser(
request.body,
email,
phoneNumber,
password,
);
console.log(user)
if (!user) {
throw new UnauthorizedException('Неверный логин или пароль');
}
return user;
}
}