Context
I am working on a NestJS application where I use Supabase authentication with JWT to protect certain API endpoints. I am using passport-jwt for authentication and @nestjs/passport for the guard. The goal is to restrict access to the /protected endpoint so that only users with a valid JWT can access it.
Setup
-
Authentication flow:
- Users sign in via
POST /auth/sign-in, which returns a JWT. - This token is then included in the
Authorizationheader (Bearer <token>) when making a request to theGET /protectedendpoint. - The
SupabaseJwtAuthGuardis applied to theprotectedendpoint to enforce authentication.
- Users sign in via
-
Code structure:
SupabaseJwtAuthGuardextendsAuthGuard('jwt')SupabaseStrategyusespassport-jwtto extract the token from theAuthorizationheader.- The JWT secret is loaded via
ConfigService(configService.get<string>('JWT_SECRET_KEY')).