jie*_*jie 2 node.js jwt typescript nestjs
我正在尝试在嵌套以下文档中使用 jwt
一切正常,但验证功能在 jwt.strategy.ts 中不起作用
这是我的 jwt.strategy.ts:
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { AuthService } from './auth.service';
import { JwtPayload } from './interfaces/jwt-payload.interface';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('JWT'),
secretOrKey: 'secretKey',
});
}
async validate(payload: JwtPayload) {
console.log(payload)
// const user = await this.authService.validateUser(payload);
// if (!user) {
// throw new UnauthorizedException();
// }
// return user;
}
}
Run Code Online (Sandbox Code Playgroud)
auth.module.ts:
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secretOrPrivateKey: 'secretKey',
signOptions: {
expiresIn: 3600,
},
}),
],
providers: [AuthService, JwtStrategy],
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)
app.module.ts:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './user/user.module';
import { GraphQLModule } from '@nestjs/graphql';
import { AuthModule } from './auth/auth.module';
@Module({
imports: [
TypeOrmModule.forRoot(),
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
}),
AuthModule,
UserModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
当我在邮递员中请求时,我没有任何日志,它似乎没有输入此验证功能。:
对不起,我的英文不好,这是我第一次使用stackoverflow,谢谢你的帮助
| 归档时间: |
|
| 查看次数: |
3398 次 |
| 最近记录: |