Hello everyone, I have a very simple doubt but that has complicated me a bit the development of my application, and basically is, how can I make an "endpoint" or better said, solve to be able to update the access token based on the refresh token? I am having a problem and it is that I never get to the JwtRefreshStrategy.
I will attach code to show.
JwtRefreshGuard
@Injectable()
export class JwtRefreshGuard extends AuthGuard('jwt-refresh') {
constructor(private readonly reflector: Reflector) {
super();
}
getRequest(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);
const req = ctx.getContext().req;
return req;
}
async canActivate(context: ExecutionContext): Promise<boolean> {
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
context.getHandler(),
context.getClass(),
]);
if (isPublic) return true;
return super.canActivate(context) as Promise<boolean>;
}
}
JwtRefreshStrategy
@Injectable()
export class JwtRefreshStrategy extends PassportStrategy(
Strategy,
'jwt-refresh',
) {
constructor(
@Inject(EnvConfig.KEY) private configService: ConfigType<typeof EnvConfig>,
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: configService.SECURITY.JWT_REFRESH_TOKEN,
passReqToCallback: true,
});
}
async validate(request: Request, payload: JwtPayload): Promise<any> {
const refreshToken = request
.get('Authorization')
.replace('Bearer', '')
.trim();
if (!refreshToken) throw new ForbiddenException('Refresh token malformed');
return { ...payload, refreshToken };
}
}