Lightweight middleware to add basic-auth protection onto your Next.js site.
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.
Install this package with npm.
npm i @phntms/next-basic-authimport 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>
);
}
}ctx: Required - TheDocumentContextprovided bygetInitialProps.config: Optional -BasicAuthMiddlewareConfigobject which allows you to change the default configuration.
You can override the configuration using these options...
name: The username required for login, defaults toadmin.pass: The password required for login, defaults topassword.realm: The realm used for the basic-auth, defaults tosite.message: The message to show upon unsuccessful login, defaults to401 Access Denied.