-
As far as I can tell, the Does this mean the browser could be using an old token to identify itself if a call to a protected route(with the auth gaurd which verifies) is never made? I'm looking for a way to essentially check the validity of tokens upon each call and to apply it globally utilizing the Some guard globally that says {
provide: APP_GUARD,
useClass: ValidSessionGuard,
}, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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.
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') {
throw new UnauthorizedException();
}
if (err) {
throw err || new UnauthorizedException();
}
return user;
}
} Then in your ...
providers: [
AppService,
AppResolver,
DateScalar,
{
provide: APP_GUARD,
useClass: OptionalAuthGuard,
},
], |
Beta Was this translation helpful? Give feedback.
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