DEV Community

Kevin Merckx
Kevin Merckx

Posted on • Originally published at blog.merckx.fr on

How to protect endpoints of a Nestjs application - Revisited

How to protect endpoints of a Nestjs application - Revisited

Two years ago, I wrote a post about protecting API endpoints of a Nestjs application. To summarize it:

  • use the declarative approach of Nestjs and protect the whole application by default behind a guard.
  • provide your project with a decorator that you can add on controllers and controllers' methods to opt out of the default guard protection.

I realized there was a nicer syntax than @SetMetadata(AUTH_GUARD_CONFIG, { disabled: true } as AuthGuardConfig). Simply create a function that does it, just with a much better name:

export const AllowUnauthorized = () => SetMetadata(AUTH_GUARD_CONFIG, { disabled: true } as AuthGuardConfig);
Enter fullscreen mode Exit fullscreen mode

That's it!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay