60
loading...
This website collects cookies to deliver better user experience
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { AuthGuard } from './guards/auth-guard';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// here we guard the whole application with one Guard
app.useGlobalGuards(new AuthGuard());
await app.listen(3000);
}
bootstrap();
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class AuthGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
return false;
}
}
/login
endpoint. In that case, you need to specifically tell the guard to not do anything for this route. You preferably should do it the Nestjs way. Here is one.SetMetadata
that you can add to endpoints to add metadata to the execution context. Therefore, if we add information on a specific route or controller, we can retrieve it from the guard.import { Controller, Get, SetMetadata } from '@nestjs/common';
import { AuthGuardConfig, AUTH_GUARD_CONFIG } from './guards/auth-guard';
@Controller()
export class AppController {
@Get('guarded')
guarded(): string {
return 'This is open.';
}
@Get('open')
@SetMetadata(AUTH_GUARD_CONFIG, { disabled: true } as AuthGuardConfig)
open(): string {
return 'This is open.';
}
}
/guarded
is guarded by the guard by default. The second one is not, as specified by the @SetMetadata(AUTH_GUARD_CONFIG, { disabled: true })
.import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Observable } from 'rxjs';
export interface AuthGuardConfig {
disabled?: boolean;
}
export const AUTH_GUARD_CONFIG = Symbol('AUTH_GUARD_CONFIG');
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const handlerConfig = this.reflector.get<AuthGuardConfig>(
AUTH_GUARD_CONFIG,
context.getHandler(),
);
const controllerConfig = this.reflector.get<AuthGuardConfig>(
AUTH_GUARD_CONFIG,
context.getClass(),
);
if (controllerConfig?.disabled || handlerConfig?.disabled) {
return true;
}
return false;
}
}
AUTH_GUARD_CONFIG
. It does it at the controller level with context.getClass()
and at the method handler level context.getHandler()
If a route specifies this metadata and puts the disabled
parameter to true
, the guard will read it and return true and therefore authorize the access. If a controller does it, all the routes of this controller will be authorized.import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { AuthGuard } from './guards/auth-guard';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalGuards(new AuthGuard(app.get(Reflector)));
await app.listen(3000);
}
bootstrap();