Open Serverless Auth Toolkit for Next.js

0.2.0 · active · verified Wed Apr 22

This package, `open-serverless-auth` (current version 0.2.0), provides a toolkit for integrating Next.js applications with a centralized Open Serverless Auth hub. It includes an Edge Middleware for protecting downstream applications by redirecting unauthorized users, and Server Action/Component helpers like `getUserData()` to retrieve authenticated user information. Its primary differentiator is simplifying centralized authentication architecture across subdomains using cross-subdomain cookies, abstracting away complex login UI development on client apps. It's designed to work seamlessly with Next.js App Router and assumes a peer dependency on `next` version 13 or higher. The current version suggests it's relatively new, likely in active development with potentially frequent updates, although a specific release cadence isn't stated. The core value proposition is enabling easy setup of a centralized authentication system, offloading authentication concerns from individual client applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up Edge Middleware for authentication and retrieving user data in a Next.js Server Component.

import { createAuthMiddleware, getUserData } from 'open-serverless-auth';
import { redirect } from 'next/navigation';

// --- src/middleware.ts ---
export const middleware = createAuthMiddleware({
  // The domain of your central auth hub, e.g., 'auth.yourdomain.com'
  // Falls back to process.env.NEXT_PUBLIC_DOMAIN if not provided.
  // domain: process.env.NEXT_PUBLIC_DOMAIN,

  // For local development only: Bypasses auth and injects a dummy user.
  // This is automatically ignored in production environments.
  devBypassUser: {
    id: "local-dev-id",
    email: "developer@example.com",
    role: "ADMIN",
    rules: { customPermission: true }
  }
});

export const config = {
  // Protect all routes except API routes, static assets, and favicon.ico
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};

// --- src/app/dashboard/page.tsx (Server Component) ---
export default async function DashboardPage() {
  const user = await getUserData();

  if (!user) {
    // Redirect unauthenticated users to a login page or an unauthorized page
    redirect('/unauthorized');
  }

  return (
    <div>
      <h1>Welcome back, {user.email}!</h1>
      <p>Your role: {user.role}</p>
      <p>Permissions: {JSON.stringify(user.rules)}</p>
    </div>
  );
}

view raw JSON →