Files
main.tsmain
What you'll learn
- Implement CanActivate and choose between returning false and throwing, and what the client gets for each
- Read handler metadata in a guard with the Reflector and a decorator made by Reflector.createDecorator
- Bind a guard to a controller with @UseGuards and mark individual handlers with a metadata decorator
Press Run tests to start the app. Its log appears here.Graded endpoints
A handler without @Roles requires nothing, so the guard lets everyone through
Roles are required and there is no user at all: the guard throws UnauthorizedException, a 401
bob is a reader, not an admin: the guard returns false and Nest answers 403
ada is an admin
Reading requires nothing, so a reader can
Deleting requires the admin role, and there is no user: 401
Signed in, and still not allowed: 403
The deleted cat comes back