@basictech/nextjs package includes middleware utilities to protect routes and require authentication before accessing certain pages.
Quick Setup
Create amiddleware.ts file in your project root:
middleware.ts
- Routes matching
/dashboard/*,/settings/*, or/api/protected/*require authentication - Unauthenticated users are redirected to
/login - The return URL is preserved so users can be redirected back after signing in
Configuration Options
createBasicMiddleware
The main function to create authentication middleware.Options
| Option | Type | Default | Description |
|---|---|---|---|
protectedRoutes | string[] | [] | Routes that require authentication. Supports glob patterns like /dashboard/* |
publicRoutes | string[] | ['/login', '/signup', '/auth/*'] | Routes that are always public (bypass auth check) |
signInUrl | string | '/login' | Where to redirect unauthenticated users |
afterSignInUrl | string | '/' | Where to redirect after successful sign-in |
tokenCookieName | string | 'basic_access_token' | Cookie name for the access token |
fullTokenCookieName | string | 'basic_token' | Cookie name for the full token object |
Route Matching
Routes support simple glob patterns with* wildcard:
Alternative: Simple Auth Middleware
For simpler use cases, usewithBasicAuth which applies default settings:
middleware.ts
/login for any matched routes.
Utility Functions
getAuthFromRequest
Check authentication status from a request:getReturnUrl
Get the return URL from search params (useful on login pages):Full Example
middleware.ts
Login Page Example
Create a login page that handles the return URL:app/login/page.tsx

