Using global auth guard doesn't work
See original GitHub issueI’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:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Let’s try:
What is the best/nest.js way to add route exceptions, so that anonymous routes can also be implemented?