Middleware
Next.js middleware for protecting routes and route groups with automatic authentication checks.
Overview
AuthSafe provides two middleware functions for route protection: createAuthMiddleware() for full route protection with redirects, and authMiddleware() for simple authentication check without enforcement. Both middleware functions work with Next.js Edge Runtime for optimal performance.
Basic Usage
import { createAuthMiddleware } from 'authsafe-nextjs/server';
export default createAuthMiddleware({
authConfig: {
clientId: process.env.AUTHSAFE_CLIENT_ID,
domain: process.env.AUTHSAFE_DOMAIN,
},
protectedRoutes: ['/dashboard/:path*', '/settings/:path*'],
publicRoutes: ['/'],
});