Is there a way to retrieve the header variables or request body in a strategy?
The JWT structure within the JwtStrategy currently appears as follows:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(
private readonly appConfigService: AppConfigService,
private readonly usersService: UsersService
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: appConfigService.configs.JWT_SECRET_KEY
});
}
async validate(payload: { id: string }) {
const user = await this.usersService.findBusinessUserById(
'BUSINESS_ID', // TODO: replace this with business id from the request
payload.id
);
if (user) {
return user;
}
return null;
}
}
The JwtAuthGuard is structured like this:
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
constructor(private readonly reflector: Reflector) {
super();
}
canActivate(
context: ExecutionContext
): boolean | Promise<boolean> | Observable<boolean> {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass()
]);
return isPublic ? true : super.canActivate(context);
}
getRequest(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);
const req = ctx.getContext().req;
return req;
}
}
An error occurs when returning anything other than the request object from the guards:
TypeError: Cannot read properties of undefined (reading 'authorization')
at JwtStrategy._jwtFromRequest
Is there a method to access the request body or header variables within the strategy?