Auth Middleware
Auth middleware allows you to implement custom authentication and authorization logic that runs before other rules in your page rules chain. This enables you to protect routes, implement OAuth flows, or add any custom authentication logic to your site.
Getting Started
To create auth middleware:
- Navigate to the “Compute > Auth Middleware” section in the QuantCDN dashboard
- Create a new middleware function using the code editor
- Apply the middleware to specific routes using Page Rules
How It Works
Auth middleware functions run before other rules in your page rules chain. They must return a response with:
- Status code 201 to indicate successful authentication and continue to the next rule
- Any other status code to halt the request (typically 401 for unauthorized or 302 for redirects)
Examples
Basic Authentication
This example implements simple username/password authentication using HTTP Basic Auth:
JWT Authentication
This example validates a JWT token from a cookie:
OAuth Integration
Auth middleware can implement OAuth flows with providers like Google, Apple, or GitHub. Here’s a simplified OAuth flow structure:
Prebuilt Examples
When creating a new auth middleware function, you can select from several prebuilt examples in the code editor. These examples provide ready-to-use authentication implementations for popular platforms:
- Apple Sign In - Complete implementation of Apple’s OAuth2 flow including JWT validation
- Google Sign In - OAuth2 implementation for Google authentication
- X (Twitter) - OAuth 1.0a implementation for X/Twitter authentication
- Auth0 - Integration with Auth0’s Universal Login
- Basic Auth - Simple username/password authentication
- JWT Validation - Token-based authentication using JWTs
To use a prebuilt example:
- Click “New Auth Middleware” in the Compute section
- Select an example from the dropdown menu in the code editor
- Replace the placeholder values (client IDs, secrets, etc.) with your own credentials
- Save and apply the middleware using Page Rules
Applying Auth Middleware
To apply auth middleware to routes:
- Create a new Page Rule
- Configure the matching criteria (domains, paths, etc.)
- Select “Authentication” as the action
- Choose your auth middleware function
Multiple auth middleware functions can be created and applied to different routes as needed.
Chaining Auth Middleware
Multiple auth middleware functions can be chained together to create sophisticated authentication flows. Each middleware in the chain must return a 201 status code for the next middleware to execute.
Common use cases for chaining include:
- Multi-factor authentication (MFA/2FA)
- Email verification
- Additional security checks
- Role-based access control
Example: Multi-Step Authentication Flow
Here’s an example of how you might structure a secure authentication chain:
-
Primary Authentication
- Validates the user’s initial login credentials
- Checks if the user has a valid session token
- Sets necessary session cookies
-
Email Verification Layer
- Checks if the user’s email is verified
- Redirects unverified users to a verification page
- Allows verified users to proceed
-
Two-Factor Authentication
- Checks if 2FA is required for the user
- Prompts for and validates 2FA codes
- Maintains 2FA session state
Implementing the Chain
To implement a chain of auth middleware:
- Create each middleware function separately in the Compute > Auth Middleware section
- Create multiple Page Rules with the same matching criteria
- Order the rules so they execute in sequence:
- Rule 1: Primary Authentication
- Rule 2: Email Verification Check
- Rule 3: Two-Factor Authentication
- Rule 4: Protected Content/Proxy/etc.