← Back to archive

Repo of the Day

jajaperson/nestjs-auth0

Published: Sep 7, 2022

Open repository ↗

Nest Auth0 via Passwort - Example App

Summary

jajaperson/nestjs-auth0 is a NestJS template that wires up Auth0 authentication using Passport. It combines jwks-rsa, passport-jwt, and @nestjs/passport so a Nest app can verify JWT bearer tokens issued by Auth0 with minimal boilerplate. The repository is positioned as a starting point engineers can clone and adapt rather than a drop-in library.

What it is useful for

This template fits engineers who already have, or are starting, a NestJS service and need to lock down routes with Auth0-issued JWTs. Concrete uses include:

  • Bootstrapping a new NestJS API that should accept Auth0 access tokens as its primary auth method.
  • Replacing ad-hoc JWT verification code with a strategy that fetches signing keys from Auth0's JWKS endpoint, so the app does not hardcode a shared secret and can rotate keys without redeploying.
  • Studying a working example of @UseGuards(AuthGuard()) on a Nest controller, useful for engineers who learn best from a runnable reference.
  • Standing up integration tests that exercise real token-validation code paths, since the project ships with both unit and e2e Jest scripts.

How engineers can use it

The README documents a straightforward setup:

  1. Clone with git clone --depth 1 https://github.com/jajaperson/nestjs-auth0.git and run npm install.
  2. Create a .env file (a .env.example is included) with AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, and AUTH0_AUDIENCE. These values are read via @nestjs/config.
  3. Run with npm run start:dev for watch mode, npm run start:prod for production, and npm run test or npm run test:e2e for tests.

The interesting code lives in src/auth/. jwt.strategy.ts builds the JwtStrategy: it uses jwks-rsa to pull Auth0's public keys and passport-jwt to verify incoming bearer tokens, then runs a validate step that checks required scopes against the decoded JwtPayload. auth.module.ts exports both PassportModule and JwtStrategy and registers the strategy as the default, so controllers in src/app.controller.ts only need @UseGuards(AuthGuard()) to be protected.

Before adopting it, engineers should note two things from the README: the package.json fields (repository, homepage, bugs, description) are template boilerplate meant to be cleaned up after forking, and the project is described as a template rather than a published library. The README also does not document a full walkthrough of running the app against a live Auth0 tenant, so for tenant configuration and callback URLs the linked Nest and Auth0 docs are the next reference.