question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using global auth guard doesn't work

See original GitHub issue

I’m submitting a…


[ ] Regression 
[] Bug report
[ ] Feature request
[x] Documentation issue or request
[] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

  const app: INestApplication & INestFastifyApplication = await NestFactory.create(AppModule, new FastifyAdapter(), {
    logger: new AppLogger('root')
  });
  app.setGlobalPrefix('v3');
  app.useGlobalGuards(AuthGuard('jwt'));
  await app.listen(4200);

This allows anyone to access my routes. However when I decorate my routes with the UseGuards decorator it works as intended:

  @Get('profile/:tag')
  @UseGuards(AuthGuard('jwt'))
  public getProfile(@Param() params: { tag: string }): {} {
    return this.xService.getProfile(params.tag);
  }

Expected behavior

  app.useGlobalGuards(AuthGuard('jwt'));

I expected this to protect my whole app just as it worked with the decorated route.

Environment


Nest version: 5.0.0.-rc4 & "@nestjs/passport": "^1.0.10",

 
For Tooling issues:
- Node version: 8.x
- Platform:  Windows

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
kamilmysliwieccommented, May 11, 2018

Let’s try:

app.useGlobalGuards(new (AuthGuard('jwt')));
5reactions
ghostcommented, Sep 29, 2018

What is the best/nest.js way to add route exceptions, so that anonymous routes can also be implemented?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nest.js: Global AuthGuard but with exceptions
You can actually set metadata for the global AuthGuard so it can determine if it should allow an unauthorized request.
Read more >
Guards | NestJS - A progressive Node.js framework
Guards. A guard is a class annotated with the @Injectable() decorator, which implements the CanActivate interface. Guards have a single responsibility.
Read more >
Router tutorial: tour of heroes
In this tutorial, you build upon a basic router configuration to explore features such as child routes, route parameters, lazy load NgModules, guard...
Read more >
How to protect endpoints of a Nestjs application
In this post, I explain how to protect your whole application with a guard against unauthorized access and mostly how to disable the...
Read more >
How to implement JWT authentication in NestJS
In fact, the Open Web Application Security Project (OWASP) ... first need to install the Nest CLI globally with the following command:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found