feat: support JWT from query string for SSE endpoints

This commit is contained in:
2026-07-05 23:15:34 +08:00
parent 14a24a14b3
commit adb875976a

View File

@@ -2,12 +2,24 @@ import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';
import { Request } from 'express';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(config: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
jwtFromRequest: ExtractJwt.fromExtractors([
// 1. Standard Bearer header (existing behavior)
ExtractJwt.fromAuthHeaderAsBearerToken(),
// 2. SSE fallback: query string ?token=
(req: Request) => {
const token = req?.query?.token;
if (typeof token === 'string' && token.length > 0) {
return token;
}
return null;
},
]),
ignoreExpiration: false,
secretOrKey: config.get('JWT_SECRET', 'dorm-billing-jwt-secret-key-2024'),
});