Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3619

NestJS AuthGuard that handles both GraphQL and REST

$
0
0

According to the docs, in order to use AuthGuard for the auth of GraphQL resolvers, we have to override getRequest method like this:

  getRequest(context: ExecutionContext) {    const ctx = GqlExecutionContext.create(context);    return ctx.getContext().req;  }

Most of my APIs use GraphQL, but the others use REST endpoints for handling file uploads. (I referred to this) Now, I'm using two AuthGuards. One is for GraphQL in which I overrode getRequest like above. The other one is for REST whose code is completely the same except getRequest (this time I did not override it) and the way to extract user object from the request after calling canActivate.

GraphQL:

// getRequest is overriddenconst user: User = this.getRequest(context).user;

REST:

// getRequest is NOT overriddenconst { user } = context.switchToHttp().getRequest();

Is there any method I can try to combine these two AuthGuards into one?


Viewing all articles
Browse latest Browse all 3619

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>