Lightweight JWT Auth for Next.js

0.2.6 · active · verified Wed Apr 22

next-lite-auth is a lightweight, JWT-based authentication solution for Next.js applications, designed specifically for scenarios where a full database or third-party auth service is overkill. It uses static JSON for user management, loaded via environment variables, eliminating the need for database setup. The current stable version is 0.2.6. Release cadence appears to be iterative and feature-driven within the 0.x range, indicating active development. Its primary differentiators are its zero-database approach, ease of setup, and a built-in login UI, making it suitable for MVPs, internal tools, demos, and educational projects. It explicitly states it is not recommended for production environments requiring robust security or scalability, instead catering to rapid development and OSS projects where authentication can be easily toggled via environment variables.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core 3-step setup: initializing auth utilities with environment variables, exposing API handlers, wrapping the application with the client-side provider, and an example of server-side user retrieval.

import { createLiteAuth, usersFromEnv } from "next-lite-auth";
import { LiteAuthProvider } from "next-lite-auth/client";
import { cookies } from 'next/headers';

// --- 1. Create auth.ts at your project root ---
// auth.ts (server-side)
export const { handlers, middleware, getUserFromCookies } = createLiteAuth({
  users: usersFromEnv(),
  jwtSecret: process.env.LITE_AUTH_SECRET ?? 'fallback-secret-for-dev',
  enabled: process.env.LITE_AUTH_ENABLED !== "false",
});

// .env.local example
// LITE_AUTH_SECRET=your-random-secret-here
// LITE_AUTH_ENABLED=true
// LITE_AUTH_USERS=[{"email":"admin@example.com","password":"secret","role":"admin","name":"Admin"}]

// --- 2. Add one route file ---
// app/api/auth/[...liteauth]/route.ts (API Route)
// import { handlers } from "@/auth"; // Assuming '@/auth' resolves to the auth.ts file
// export const { GET, POST } = handlers;

// --- 3. Wrap root layout ---
// components/auth-provider-wrapper.tsx (Client Component)
"use client";
export function AuthProvider({ children }: { children: React.ReactNode }) {
  return (
    <LiteAuthProvider protect={["/dashboard", "/settings"]} appName="My Next App">
      {children}
    </LiteAuthProvider>
  );
}

// app/layout.tsx (Server Component)
// import { AuthProvider } from "@/components/auth-provider-wrapper";
// export default function RootLayout({ children }: { children: React.ReactNode }) {
//   return (
//     <html>
//       <body>
//         <AuthProvider>{children}</AuthProvider>
//       </body>
//     </html>
//   );
// }

// Example of server-side usage outside middleware/API routes
async function getServerSideUser() {
  const user = await getUserFromCookies(cookies());
  console.log('Server-side user:', user?.email);
}

// Call the function (e.g., in a Server Component or route handler)
getServerSideUser();

view raw JSON →