Menu

Keeping students out of staff endpoints: NestJS guards and roles in my attendance project
📰

Keeping students out of staff endpoints: NestJS guards and roles in my attendance project

DEV Community: nestjs·Ziad Taha·6 days ago
#wUtlKtn0
#devtonestjs#devto#dev#roles#role
Long article (862 words) — no AI summary yet.
Reading 0:00
15s threshold

I'm a junior full-stack developer from Alexandria, Egypt. This is another post about FaceMark, my face-recognition attendance system (a NestJS API plus a Python AI service). This one covers authorization: how the API decides that a student can see their own attendance but nobody else's, and that only staff and admins can pull class-wide records.

FaceMark has three roles, defined as a plain enum:

// src/common/enums/role.enum.ts export enum Role { ADMIN = 'admin' , STAFF = 'staff' , STUDENT = 'student' , }

Enter fullscreen mode

Exit fullscreen mode

The system is three small pieces: a JWT guard that answers "who are you", a decorator that answers "who is allowed here", and a roles guard that compares the two.

The JWT guard

// src/auth/guards/jwt-auth.guard.ts @Injectable ( ) export class JwtAuthGuard extends AuthGuard ( 'jwt' ) { }

Enter fullscreen mode

Exit fullscreen mode

That's the whole file. It extends Passport's JWT AuthGuard: no valid token, no request.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.