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 🙏

© 2024 – Pkg Stats / Ryan Hefner

staging-next

v0.0.4

Published

Next.js integration for staging password protection

Downloads

290

Readme

staging-next

Next.js integration for staging password protection middleware.

npm version Bundle Size License: MIT TypeScript

Overview

Edge Runtime compatible password protection for Next.js applications.

Live Demo

Try out our Next.js demo (password: demo): 🚀 staging-next.vercel.app

Installation

npm install staging-next
# or
yarn add staging-next
# or
pnpm add staging-next

Usage

Create a middleware file in your Next.js project:

// middleware.ts
import staging from 'staging-next';

export const middleware = staging({
  password: process.env.STAGING_PASSWORD,
  siteName: "My Protected Site"
});

Features

  • Edge Runtime compatible
  • Native Next.js middleware integration
  • Cookie-based session handling
  • Custom route matcher support
  • No Node.js dependencies

Configuration

See the main documentation for base options.

Next.js-specific Options

Additional options available for Next.js:

interface StagingNextOptions extends StagingOptions {
  /** Custom matcher patterns for middleware */
  matcher?: string[];
}

Route Matching

Configure which routes should be handled by the middleware:

export const middleware = staging({
  password: process.env.STAGING_PASSWORD,
  matcher: [
    // Custom matcher patterns
    "/((?!_next|public|api).*)",
  ],
  publicRoutes: [
    "^/public(/.*)?$",
    "^/api/public(/.*)?$"
  ]
});

Advanced Middleware Configuration

Combining with Custom Auth Middleware

While we recommend using Nemo for a more robust authentication solution, according to Next.js documentation about nested middleware, you can also combine multiple middleware functions. Here's how to combine staging protection with your own authentication middleware to handle both password protection for staging environments and user authentication:

import { NextRequest, NextResponse } from "next/server";
import staging from "staging-next";
import { getToken } from "@/services/jwt";

// Your custom auth middleware implementation
async function authMiddleware(request: NextRequest) {
  // Add your auth logic here
  return NextResponse.redirect(new URL("/auth", request.url));
}

// Routes that require authentication
const authMiddlewareMatcher = [
  "/dashboard",
  "/settings",
  "/checkout",
];

// Configure staging middleware
const stagingMiddleware = staging({
  siteName: "My Protected Site",
});

// Combined middleware function
export function middleware(request: NextRequest) {
  if (authMiddlewareMatcher.some((path) => 
    request.nextUrl.pathname.startsWith(path)
  )) {
    return authMiddleware(request);
  }
  return stagingMiddleware(request);
}

Example

A complete working example is available in our repository:

License

MIT