Repo of the Day
hiro1107/nestjs-supabase-auth
Published: Nov 28, 2022
Open repository ↗Summary
nestjs-supabase-auth is a NestJS Passport strategy that lets a NestJS application verify JWTs issued by Supabase Auth on incoming requests. The README shows how to subclass SupabaseAuthStrategy, supply your Supabase URL, key, and JWT secret, and then guard NestJS (and GraphQL) endpoints so that the decoded Supabase user becomes available on the request.
What it is useful for
This package is useful when you have a NestJS backend and you want Supabase to handle authentication (sign-up, sign-in, password reset, social providers, etc.) but you still need server-side verification of the JWT that the client sends on each request. Typical scenarios include:
- A NestJS HTTP or GraphQL API whose frontend authenticates with Supabase and then calls your API with a Supabase access token.
- Resolvers or controllers that need the authenticated
SupabaseAuthUserobject (for example, to scope database queries byuser.id). - Centralizing auth checks with a standard
@UseGuards(AuthGuard('supabase'))pattern rather than hand-rolling JWT verification per route.
The README also shows how a custom GqlAuthGuard and a CurrentUser param decorator expose the user inside @nestjs/graphql resolvers, which is the main demonstrated flow.
How engineers can use it
The README documents a concrete setup. The steps below use only the commands and code shown in the README; the example also assumes you load Supabase credentials via environment variables (the README uses dotenv with env-cmd).
- Install the peer dependencies and the strategy:
(Yarn equivalents are also listed.)npm install passport passport-jwt @nestjs/passport npm install --save-dev @types/passport-jwt npm install nestjs-supabase-auth - Create a strategy class that extends
SupabaseAuthStrategyviaPassportStrategy, supplyingsupabaseUrl,supabaseKey,supabaseOptions,supabaseJwtSecret, and a JWTextractorsuch asExtractJwt.fromAuthHeaderAsBearerToken(). - Register
PassportModuleand yourSupabaseStrategyin anAuthModuleproviders list, exporting it as needed. - Protect routes with
@UseGuards(AuthGuard('supabase'))for REST controllers, or the README'sGqlAuthGuardfor GraphQL resolvers, and read the user with the documentedCurrentUserdecorator.
Documented gaps to be aware of: the README only demonstrates Bearer-token extraction from the Authorization header and a GraphQL guard; cookie- or query-based JWT extraction is not shown. Token refresh, role/permission checks, and revocation handling are not described, so you would need to handle those yourself or check the README for updates.