Skip to content

Refresh Token Strategy #458

Answered by Rykuno
Rykuno asked this question in Q&A
Discussion options

You must be logged in to vote

Essentially I wanted to verify each token so it wouldn't just refresh the access token on private routes and I could access context in any resolver without a guard. Here's how I solved it.

global-auth-guard.ts

import {
  Injectable,
  ExecutionContext,
  UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { GqlExecutionContext } from '@nestjs/graphql';

@Injectable()
export class OptionalAuthGuard extends AuthGuard('jwt') {
  getRequest(context: ExecutionContext) {
    const ctx = GqlExecutionContext.create(context);
    return ctx.getContext().req;
  }

  handleRequest(err, user, info) {
    if (info?.name === 'TokenExpiredError') {
      t…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Rykuno
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant