NodeJS 如何在护照策略中使用多个字段

zbdgwd5y  于 2023-03-29  发布在  Node.js
关注(0)|答案(2)|浏览(87)

我怎么能通过2个以上的字段作为用户名在这种情况下?我想做的电话号码和SSA登录不仅与电子邮件。

  1. export class LocalStrategy extends PassportStrategy(Strategy) {
  2. constructor(private authenticationService: AuthenticationService) {
  3. super({
  4. usernameField: 'email'
  5. });
  6. }
  7. async validate(email: string, password: string) {
  8. return this.authenticationService.getAuthenticatedUser(email, password);
  9. }
  10. }
n7taea2i

n7taea2i1#

你可以在构造函数中将passReqToCallback选项传递给你的super()调用,这将为我们的validate函数提供完整的req对象作为第一个参数,这样我们就可以随意使用它,如下所示:

  1. export class LocalStrategy extends PassportStrategy(Strategy) {
  2. constructor(private authenticationService: AuthenticationService) {
  3. super({
  4. usernameField: 'email',
  5. passReqToCallback: true
  6. });
  7. }
  8. async validate(req: any, email: string, password: string) {
  9. // Inspect the req object as you wish
  10. return this.authenticationService.getAuthenticatedUser(req.body.phone_number, req.body.SSA);
  11. }
  12. }

我把这个问题作为参考,它应该同样适用于nestjs/passport库。Using PassportJS, how does one pass additional form fields to the local authentication strategy?

irtuqstp

irtuqstp2#

试试这个。

  1. export class LocalStrategy extends PassportStrategy(Strategy) {
  2. constructor(private authenticationService: AuthenticationService) {
  3. super({
  4. usernameField: 'phone_number'
  5. });
  6. }
  7. async validate(phone_number: string, password: string) {
  8. return this.authenticationService.getAuthenticatedUser(phone_number, password);
  9. }
  10. }

我查找了有关是否可以添加新字段的信息,但在文档和论坛中没有找到任何内容

相关问题