Roles — Security · NestJS

Signed in is not the same as allowed: give each member of staff roles, carry them in the token, and let a second global guard decide, route by route, which role a request needs.

What you will learn

Read the theory for Roles

All Security lessons

All NestJS courses

loading types…

What you'll learn

  • Separate authentication (who) from authorization (what they may do), and answer 401 for the first and 403 for the second
  • Carry roles as a claim in the token and read them from request.user in a guard
  • Write a guard that reads required roles from handler and class metadata through Reflector and lets undecorated routes through
  • Order global guards so the one that needs a user runs after the one that finds it