Overview
Bypass authentication guards for public endpoints like health checks.
Features
- Skip auth guards
- Works with global guards
- Simple metadata decorator
Installation
npx hanma add public --framework nest
Code
import { SetMetadata } from "@nestjs/common";
export const IS_PUBLIC_KEY = "isPublic";
/**
* Mark a route as public, bypassing authentication guards.
* Useful when you have a global auth guard but need some endpoints public.
*
* Usage:
* @Public()
* @Get("health")
* healthCheck() {
* return { status: "ok" };
* }
*
* // In your JwtAuthGuard:
* canActivate(context: ExecutionContext) {
* const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
* context.getHandler(),
* context.getClass(),
* ]);
* if (isPublic) return true;
* // ... rest of auth logic
* }
*/
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
Usage
@Public()
@Get('health')
healthCheck() {}
Dependencies
No external dependencies required.
Frequently Asked Questions
How do I install Public Route?
Run the following command:
npx hanma add public --framework nestHow do I use this snippet?
Copy the code and paste it into your project. The snippet will be saved to
src/decorators/public.decorator.ts.Can I customize this snippet?
Yes! All snippets are copy-paste code that you own. Feel free to modify them to fit your needs.
Does this work with other frameworks?
This snippet is designed for the specific framework. Check the other framework pages for equivalent implementations.