Authentication Flow
User Authenticates with Auth0
Users authenticate through Auth0’s authentication flow (hosted login, social login, etc.).
Auth0 Issues JWT Token
Upon successful authentication, Auth0 issues a JWT token signed with RS256 algorithm.
JWT Middleware Implementation
ThecheckJwt middleware is implemented using express-jwt and jwks-rsa:
src/middlewares/checkJwt.ts
Configuration Parameters
JWKS Configuration
cache: true- Caches the signing keys to reduce requests to Auth0rateLimit: true- Enables rate limiting for JWKS requestsjwksRequestsPerMinute: 5- Limits JWKS endpoint requests to 5 per minutejwksUri- The Auth0 JWKS endpoint URL for fetching public keys
The JWKS (JSON Web Key Set) endpoint provides the public keys used to verify JWT signatures. Caching these keys improves performance and reduces load on Auth0’s servers.
JWT Validation Parameters
secret- The JWKS client that fetches and caches Auth0’s public keysaudience- The API identifier configured in Auth0 (must match token’saudclaim)issuer- Your Auth0 domain (must match token’sissclaim)algorithms- Only RS256 algorithm is accepted for token verification
Environment Variables
The following environment variables must be configured:.env
The
AUTH0_DOMAIN should include the trailing slash and the https:// protocol.Getting Your Auth0 Configuration
Create an API in Auth0
Go to the Auth0 Dashboard → Applications → APIs and create a new API. The identifier you choose will be your
AUTH0_AUDIENCE.Note Your Domain
Your Auth0 domain is shown in the dashboard (e.g.,
https://dev-abc123.us.auth0.com/).Protected Endpoints
ThecheckJwt middleware is applied to all image-related endpoints:
src/index.ts
The middleware is placed before the route handlers, ensuring authentication happens before any business logic executes.
Authentication Error Responses
When authentication fails, the middleware automatically returns appropriate error responses:Missing Token
Status:401 Unauthorized
Invalid Token
Status:401 Unauthorized
Expired Token
Status:401 Unauthorized
Wrong Audience
Status:401 Unauthorized
Dependencies
The Auth0 integration requires two npm packages:package.json
express-jwt- Express middleware for validating JWTsjwks-rsa- Library for retrieving RSA signing keys from JWKS endpoints
Why RS256 Algorithm?
Why RS256 Algorithm?
RS256 (RSA Signature with SHA-256) is an asymmetric algorithm that uses a public/private key pair. Auth0 signs tokens with a private key, and the API verifies them using the public key fetched from the JWKS endpoint. This means the API never needs access to the signing key, making it more secure than symmetric algorithms like HS256.
CORS Configuration
The API is configured to accept requests from the frontend application:src/index.ts