[英]How to pass request object through Nest.js Guards with Nest.js Passport Module and Passport Discord?
[英]How to always validate JWT without guards decorator? (Nest.js + passport)
我在项目中使用nest.js + passport + jwt + graphql。
如果有令牌,则解码信息,如果没有令牌,则希望得到未定义的信息。
始终必须有 Guards 才能接收解码的令牌。
我可以选择性地生成 401 错误吗?
@Module({ providers: [ JwtStrategy, ], imports: [ PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule.register({ secret: 'hi', signOptions: { expiresIn: '1d' }, }), ], }) export class AuthModule { } export class GqlAuthGuard extends AuthGuard('jwt') { getRequest(context: ExcutionContext) { const ctx = GqlExecutionContext.create(context) return ctx.getContext().req } } export class JwtStrategy extends PassportStrategy(Strategy) { constructor() { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: config.jwt.secret, }) } validate(payload: JwtPayload): any { return { user_id: +payload.user_id, username: payload.username } } } @Resolver(() => Test) export class TestResolver { @UseGuards(GqlAuthGuard) // I want to get validated users without guards. @Query(() => Test) startTest( @User() user: any, // req.user ) { console.log(user) } }
我不知道这是否可能,但我想要这个代码。
app.use((req, res, next) => { if (req.headers.token) { // optional try { req.user = verify(req.headers.token) } catch (err) { req.user = undefined } } next() }) app.get((req, res, next) => { if (req.user) {... } })
您可以像这样应用全局警卫
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);
const reflector = app.get(Reflector);
app.useGlobalGuards(new GqlAuthGuard(reflector));
await app.listen(process.env.PORT);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.