how to get raw jwt token in passport-jwt @nestjs/passport strategy?

55 Views Asked by At

this is my code. i want to get raw jwt token.

import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { Request } from 'express';
import { ExtractJwt, Strategy } from 'passport-jwt';


@Injectable()
export class JwtRefreshStrategy extends PassportStrategy(
  Strategy,
  'jwt-refresh',
) {
  constructor(
    configService: ConfigService,
  ) {
    super({
      jwtFromRequest: (req: Request) => {
        const refreshToken = req.cookies?.refresh_token;
        if (!refreshToken) {
          throw new HttpException('error msg', HttpStatus.UNAUTHORIZED);
        }
        // console.log(refreshToken); : original raw jwt
        return refreshToken;
      },
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('JWT_REFRESH_SECRET'),
    });
  }

  async validate(req: Request, payload: any) {
    // code with raw jwt token.
    // but i get req with decoded jwt token.
    return null;
  }
}

i can get raw jwt token in extractor. but i get payload in req of validate func.(ex {name:'john dae',exp:foobar....}

i want to use raw jwt in validate func.

1

There are 1 best solutions below

0
nayounsang On
super({
      jwtFromRequest: (req: Request) => {
        // my extractor
      },
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('JWT_REFRESH_SECRET'),
      passReqToCallback: true, 
    });

passReqToCallback works for me.