Skip to content

Public decorator

Mark routes as publicly accessible without authentication.

The @Public() decorator marks a route as publicly accessible without authentication.

The decorator is defined in src/brkpt-auth/common/decorators/public.decorator.ts:

src/brkpt-auth/common/decorators/public.decorator.ts
import { SetMetadata } from '@nestjs/common';
export const IS_PUBLIC_KEY = 'IS_PUBLIC_KEY';
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);

Authentication guards use this metadata to skip authentication for routes marked with @Public().

Apply @Public() to any route that should be accessible without an access token:

import { Controller, Get } from '@nestjs/common';
import { Public } from './brkpt-auth/common/decorators/public.decorator';
@Controller()
export class AppController {
@Public()
@Get('health')
health() {
return 'OK';
}
}

The /health route can now be accessed without authentication.

You can also apply @Public() to a controller to make all of its routes public:

@Public()
@Controller('public')
export class PublicController {}