{"id":13485191,"url":"https://github.com/labd/nextjs-basic-auth-middleware","last_synced_at":"2025-04-04T15:09:52.005Z","repository":{"id":43176051,"uuid":"248974409","full_name":"labd/nextjs-basic-auth-middleware","owner":"labd","description":"Add basic authentication to a NextJS application as middleware","archived":false,"fork":false,"pushed_at":"2023-12-31T05:25:54.000Z","size":641,"stargazers_count":215,"open_issues_count":6,"forks_count":22,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T14:09:31.804Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/labd.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-03-21T12:46:32.000Z","updated_at":"2025-01-22T01:09:48.000Z","dependencies_parsed_at":"2024-01-16T07:22:50.877Z","dependency_job_id":"19c1d300-b5d3-4a7b-a81b-6951e9f620d4","html_url":"https://github.com/labd/nextjs-basic-auth-middleware","commit_stats":{"total_commits":76,"total_committers":4,"mean_commits":19.0,"dds":0.1842105263157895,"last_synced_commit":"db07837769d1795c1eac5bdcbccd7715b71f54df"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labd%2Fnextjs-basic-auth-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labd%2Fnextjs-basic-auth-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labd%2Fnextjs-basic-auth-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labd%2Fnextjs-basic-auth-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/labd","download_url":"https://codeload.github.com/labd/nextjs-basic-auth-middleware/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198463,"owners_count":20900080,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:50.006Z","updated_at":"2025-04-04T15:09:51.980Z","avatar_url":"https://github.com/labd.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# nextjs-basic-auth-middleware\n\n\n\nAdds basic auth support to Next.js projects using the official middleware approach (with a `middleware` file).\nOptions can be set on the basic auth middleware and overridden using environment variables.\n\n## Compatibility table\n\n| Next.js version | Plugin version |\n| --------------- | -------------- |\n| Next.js \u003e=13.1  | 3.x            |\n| Next.js 12,13.0 | 2.x            |\n| Next.js 10,11   | 1.x            |\n\n\n## Installation\n\nRun either of the following commands to add the package, based on your package manager of choice:\n\n```sh\n# NPM\nnpm install nextjs-basic-auth-middleware\n\n# Yarn\nyarn add nextjs-basic-auth-middleware\n```\n\n## Usage\n\n### Next.js Middleware\nThe Next.js middleware functionality allows you to add basic auth in front of all your requests, see the [Next.js Middleware documentation](https://nextjs.org/docs/advanced-features/middleware) for more information.\n\nIt consists of 2 parts, `createNextAuthMiddleware` for checking and redirecting and `createApiPage` to create the API page that sends a 401 message.\n\nYou can use the `createNextAuthMiddleware` function to create a default middleware function that sends a `NextResponse.next()` when the auth passes:\n\n```js\n    // middleware.ts\n    import { createNextAuthMiddleware } from 'nextjs-basic-auth-middleware'\n\n    export const middleware = createNextAuthMiddleware(options)\n\n    export const config = {\n        matcher: ['/(.*)'], // Replace this with your own matcher logic\n    }\n```\n\n**Optional**\n\nYou can also use the `nextBasicAuthMiddleware` function to check basic auth in a bigger middleware function:\n\n```js\n    import { nextBasicAuthMiddleware } from 'nextjs-basic-auth-middleware'\n\n    export const middleware = (req) =\u003e {\n        nextBasicAuthMiddleware(options, req)\n\n        // Your other middleware functions here\n\n        return NextResponse.next()\n    }\n\n```\n\n\u003e :warning: The middleware will still return a 401 and will quit processing the rest of the middleware. Add this middleware after any required work.\n\n\n### Setting environment variables\nIf you want to override credentials you can use the `BASIC_AUTH_CREDENTIALS` environment variable:\n\n```sh\n# Enables basic auth for user `test` with password `password`\nBASIC_AUTH_CREDENTIALS=user:password\n\n# You can set multiple accounts using `|` as a delimiter\nBASIC_AUTH_CREDENTIALS=user:password|user2:password2\n```\n\n## API\n### nextBasicAuthMiddleware()\n```nextBasicAuthMiddleware(req: NextApiRequest, res: http.ServerResponse, options)```\n\nThe options object can contain any of the following options:\n\noption | description | default value\n------ | ----------- | -------------\n`pathname`| The path that the middleware redirects to | `/api/auth`\n`users`| A list of users that can authenticate | `[]`\n`message`| Message to display to the user when authentication fails | `Authentication failed`\n`realm` | Realm to show in the WWW-Authenticate header | `protected`\n\nThe user object consists of the following required fields:\n\nfield | description | type\n----- | ----------- | ----\n`name`| The username | `string`\n`password`| The password | `string`\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flabd%2Fnextjs-basic-auth-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flabd%2Fnextjs-basic-auth-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flabd%2Fnextjs-basic-auth-middleware/lists"}