首页 > 解决方案 > NestJS 身份验证 JWT Cookie

问题描述

任何人都可以帮助我,我不知道哪里错了,但我的 jwt 身份验证不起作用,它不运行函数 validate 甚至我的请求?.cookies?.["jwt"] 存在并且总是返回状态代码 401。这是我的代码: jwt.strategy.ts:

    `@Injectable()
    export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(  ) {
    super({
      ignoreExpiration:false,
      secretOrKey: 'secret',
       jwtFromRequest: ExtractJwt.fromExtractors([ (request: Request) => {
         return request?.cookies?.["jwt"];
       }])
    })
  }

  async validate (payload:any) {
    console.log(1);
    return {mail: payload.mail  };
  }
}`

auth.module.ts:

`@Module({
  imports:[PassportModule,
    UserModule,
    JwtModule.register({
      secret:'secret',
      signOptions:{expiresIn:'60s'}
    })
  ],
  providers: [AuthService,LocalStrategy,JwtStrategy],
  exports:[AuthService]
})
export class AuthModule {}`

标签: cookiesnestjs-passport

解决方案


推荐阅读