{"library":"nextjs-basic-auth","title":"Next.js Basic Authentication","description":"nextjs-basic-auth provides a straightforward method for integrating HTTP Basic Authentication directly into Next.js applications on a per-route basis. Currently at version 0.1.3, this package is designed for simplicity, offering an `initializeBasicAuth` function that returns a check function to be awaited within `getServerSideProps`. It explicitly requires developers to opt out of Next.js's static generation for any authenticated pages, as it needs a server-side context to perform credential checks for each request. Its release cadence appears stable and infrequent, focusing on a specific, well-defined security concern for Next.js page routes. Key differentiators include its direct integration with `getServerSideProps` for page-level protection, in contrast to middleware-based solutions (like `nextjs-basic-auth-middleware`) or more comprehensive authentication libraries (like NextAuth.js) that handle broader authentication flows including sessions, OAuth, and database integration.","language":"javascript","status":"maintenance","last_verified":"Wed Apr 22","install":{"commands":["npm install nextjs-basic-auth"],"cli":null},"imports":["import initializeBasicAuth from 'nextjs-basic-auth'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import initializeBasicAuth from 'nextjs-basic-auth';\n\nconst users = [\n  { user: 'user1', password: 'toocool' },\n  { user: 'admin', password: process.env.ADMIN_PASSWORD ?? 'password' }, // Use env vars for production!\n];\n\n// Initialize the basic auth checker once globally or per module\nconst basicAuthCheck = initializeBasicAuth({\n  users: users\n});\n\n// In your Next.js page file (e.g., pages/protected.js or app/protected/page.js for Pages Router or App Router with getServerSideProps emulation)\nexport async function getServerSideProps(ctx) {\n  const { req, res } = ctx;\n\n  // Await the auth check. If authentication fails, it will send a 401 response and prompt the user.\n  await basicAuthCheck(req, res);\n\n  // If authentication passes, proceed with rendering the page\n  return {\n    props: {},\n  };\n}\n\n// If using the App Router, this pattern is less direct and would typically involve server components or middleware.\n// This example is primarily for the Pages Router where getServerSideProps is common.","lang":"javascript","description":"Demonstrates initializing basic authentication with a list of users and applying it to a Next.js page using `getServerSideProps` to protect the route.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}