Skip to content

Lightweight middleware to add basic-auth protection onto your Next.js site.

License

Notifications You must be signed in to change notification settings

phantomstudios/next-basic-auth

Repository files navigation

next-basic-auth

NPM version Actions Status PR Welcome

Lightweight middleware to add basic-auth protection onto your Next.js site.

Introduction

Do you have a site that is still in development or need to protect non production environments? Then this basic library is for you. It allows you to protect all your pages in one go by adding this middleware to your _document template.

Installation

Install this package with npm.

npm i @phntms/next-basic-auth

Usage

import basicAuth from "@phntms/next-basic-auth";
import Document, {
  DocumentContext,
  Head,
  Html,
  Main,
  NextScript,
} from "next/document";

const authConfig = {
  name: "john",
  pass: "letmein",
  message: "Go away!",
};

export default class MyDocument extends Document {
  static async getInitialProps(ctx: DocumentContext) {
    await basicAuth(ctx, authConfig);
  }

  render() {
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

basicAuth Arguments

  • ctx : Required - The DocumentContext provided by getInitialProps.
  • config : Optional - BasicAuthMiddlewareConfig object which allows you to change the default configuration.

Configuration

You can override the configuration using these options...

  • name : The username required for login, defaults to admin.
  • pass : The password required for login, defaults to password.
  • realm : The realm used for the basic-auth, defaults to site.
  • message : The message to show upon unsuccessful login, defaults to 401 Access Denied.