npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nguard

v0.5.0

Published

Next.js 16+ session management library with built-in auth() function, callback-based architecture, and zero-config SessionProvider

Readme

Nguard Logo

Nguard

Next.js 16+ Session Management Library

Zero-config authentication • JWT-based sessions • Works with any backend

InstallationQuick StartHooksDocs


Installation

npm install nguard
npx nguard-setup

That's it! The wizard will automatically create:

  • lib/auth.ts - Server authentication utilities
  • ✅ API routes - /api/auth/login, /api/auth/logout, /api/auth/validate
  • proxy.ts - Next.js 16 middleware
  • .env.local.example - Configuration template

Quick Start

1. Wrap your app with SessionProvider

// app/layout.tsx
'use client';

import { SessionProvider } from 'nguard/client';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <SessionProvider>{children}</SessionProvider>
      </body>
    </html>
  );
}

2. Get session in Server Components

// app/dashboard/page.tsx
import { auth } from '@/lib/auth';

export default async function Dashboard() {
  const session = await auth();

  if (!session) {
    return <div>Please log in</div>;
  }

  return <div>Welcome, {session.email}</div>;
}

3. Use hooks in Client Components

// app/components/login.tsx
'use client';

import { useLogin } from 'nguard/client';

export function LoginForm() {
  const { login, isLoading } = useLogin();

  const handleSubmit = async (e) => {
    e.preventDefault();
    const formData = new FormData(e.currentTarget);

    const response = await login({
      email: formData.get('email'),
      password: formData.get('password'),
    });

    if (response.session) {
      console.log('Logged in!');
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <input type="email" name="email" placeholder="Email" required />
      <input type="password" name="password" placeholder="Password" required />
      <button disabled={isLoading}>
        {isLoading ? 'Logging in...' : 'Login'}
      </button>
    </form>
  );
}

Hooks

useSession()

Get the current session

const { session, loading } = useSession();

useLogin()

Login with credentials

const { login, isLoading } = useLogin();
const response = await login({ email, password });

useLogout()

Logout the user

const { logout, isLoading } = useLogout();
await logout();

useSessionUpdate()

Update session data

const { updateSession, isLoading } = useSessionUpdate();
await updateSession(newSessionData);

useValidateSession()

Check if session is valid

const { validate, isValid, validationResult } = useValidateSession();
await validate();

Server-Side

auth()

Get session in Server Components

import { auth } from '@/lib/auth';

const session = await auth();

createSession()

Create a new session

import { nguard } from '@/lib/auth';

const { session, setCookieHeader } = await nguard.createSession({
  id: 'user-123',
  email: '[email protected]',
  role: 'admin',
  expires: Date.now() + 24 * 60 * 60 * 1000,
});

clearSession()

Clear the session

const cookieHeader = nguard.clearSession();

logout()

Handle logout with cleanup

import { nguard } from '@/lib/auth';

const cookieHeader = await nguard.logout(session);

validateSession()

Validate a session token

const session = await nguard.validateSession(cookieString);

Features

  • Zero-config - SessionProvider needs no callbacks
  • TypeScript - 100% type-safe
  • JWT Sessions - Secure, stateless authentication
  • Server Components - Works with async/await
  • Client Hooks - useSession, useLogin, useLogout
  • Middleware - Built-in role-based access control
  • Session Validation - Check session validity anytime
  • Any Backend - Works with Spring, Express, Django, etc.
  • Next.js 16+ - Compatible with latest Next.js

Architecture

Next.js App
    ↓
SessionProvider (manages session state)
    ↓
useLogin/useLogout/useSession hooks
    ↓
API Routes (/api/auth/login, /api/auth/logout, etc)
    ↓
Your Backend (Spring, Express, Django, etc)
    ↓
JWT Token ← Session Data
    ↓
HTTP-only Cookie

Docs

Turkish Docs

Example Response

Your backend determines the response structure:

// Login endpoint returns
{
  session: {
    id: 'user-123',
    email: '[email protected]',
    role: 'admin',
    permissions: ['read', 'write']
  }
}

Environment Variables

NGUARD_SECRET=your-32-character-secret
BACKEND_API_URL=http://localhost:8080/api
NODE_ENV=development

Generate a secret:

openssl rand -base64 32

Security

  • ✅ HTTP-only cookies
  • ✅ CSRF protection
  • ✅ Secure cookie flags
  • ✅ JWT validation
  • ✅ Session expiration

License

MIT


Ready to get started?

npm install nguard && npx nguard-setup