• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add basic authentication to a NextJS application as middleware

nextjs-basic-auth-middleware

Adds basic auth support to Next.js projects using the official middleware approach (with a middleware file). Options can be set on the basic auth middleware and overridden using environment variables.

Compatibility table

Next.js version Plugin version
Next.js >=13.1 3.x
Next.js 12,13.0 2.x
Next.js 10,11 1.x

Installation

Run either of the following commands to add the package, based on your package manager of choice:

# NPM
npm install nextjs-basic-auth-middleware

# Yarn
yarn add nextjs-basic-auth-middleware

Usage

Next.js Middleware

The Next.js middleware functionality allows you to add basic auth in front of all your requests, see the Next.js Middleware documentation for more information.

It consists of 2 parts, createNextAuthMiddleware for checking and redirecting and createApiPage to create the API page that sends a 401 message.

You can use the createNextAuthMiddleware function to create a default middleware function that sends a NextResponse.next() when the auth passes:

    // middleware.ts
    import { createNextAuthMiddleware } from 'nextjs-basic-auth-middleware'

    export const middleware = createNextAuthMiddleware(options)

    export const config = {
        matcher: ['/(.*)'], // Replace this with your own matcher logic
    }

Optional

You can also use the nextBasicAuthMiddleware function to check basic auth in a bigger middleware function:

    import { nextBasicAuthMiddleware } from 'nextjs-basic-auth-middleware'

    export const middleware = (req) => {
        nextBasicAuthMiddleware(options, req)

        // Your other middleware functions here

        return NextResponse.next()
    }

โš ๏ธ The middleware will still return a 401 and will quit processing the rest of the middleware. Add this middleware after any required work.

Setting environment variables

If you want to override credentials you can use the BASIC_AUTH_CREDENTIALS environment variable:

# Enables basic auth for user `test` with password `password`
BASIC_AUTH_CREDENTIALS=user:password

# You can set multiple accounts using `|` as a delimiter
BASIC_AUTH_CREDENTIALS=user:password|user2:password2

API

nextBasicAuthMiddleware()

nextBasicAuthMiddleware(req: NextApiRequest, res: http.ServerResponse, options)

The options object can contain any of the following options:

option description default value
pathname The path that the middleware redirects to /api/auth
users A list of users that can authenticate []
message Message to display to the user when authentication fails Authentication failed
realm Realm to show in the WWW-Authenticate header protected

The user object consists of the following required fields:

field description type
name The username string
password The password string

More Repositories

1

django-cognito-jwt

An Authentication backend for Django Rest Framework for AWS Cognito JWT tokens
Python
179
star
2

wagtailstreamforms

What happened when a FormBuilder met a StreamField
Python
156
star
3

django-oscar-wagtail

Wagtail integration for Oscar Commerce (or Oscar Commerce integration for Wagtail?)
Python
101
star
4

wagtail-2fa

2 Factor Authentication for Wagtail
Python
84
star
5

terraform-provider-commercetools

Terraform provider for commercetools
Go
64
star
6

django-healthchecks

Simple Django app/framework to publish health check for monitoring purposes
Python
60
star
7

django-iam-dbauth

Django backends to support IAM database authentication for AWS
Python
44
star
8

mach-composer

MACH composer is an orchestration tool for modern MACH ecosystems, powered by Terraform infrastructure-as-code underneath.
Go
39
star
9

django-session-timeout

Add timestamp to sessions to expire them
Python
36
star
10

commercetools-go-sdk

Go SDK for commercetools
Go
27
star
11

commercetools-node-mock

Mock for the commercetools rest api
TypeScript
19
star
12

ecs-deplojo

Deployment tool for Amazon ECS
Python
19
star
13

commercetools-python-sdk

Commercetools Python SDK
Python
17
star
14

react-abode

React micro-frontend framework allowing you to host multiple React components from HTML.
TypeScript
15
star
15

python-param-store

Python module to store secrets in secret stores
Python
15
star
16

django-perms-provisioner

Provision Django instances with groups and permissions
Python
13
star
17

terraform-provider-contentstack

Terraform provider for Contentstack
Go
9
star
18

wagtail-audit-trail

Python
8
star
19

terraform-provider-sonarqube

Terraform Provider for SonarQube
Go
8
star
20

react-loqate

React implementation of the Loqate API.
TypeScript
8
star
21

django-postcode-lookup

Generic API for postcode lookups
Python
8
star
22

terraform-provider-amplience

Terraform provider for Amplience
Go
7
star
23

commercetools-mock-server

Docker based mock server for the commercetools API
Dockerfile
7
star
24

pytest-bdd-splinter

Python
6
star
25

wagtail-site-inheritance

Support for site inheritance in Wagtail
Python
6
star
26

schema-generate

A library and CLI tool to generate various schemas based off of TypeScript interfaces.
TypeScript
5
star
27

changie-release-action

GitHub action for Changie
4
star
28

terraform-provider-taskrunner-aws-ecs

Terraform Provider for running ECS tasks
Go
4
star
29

graphql-codegen-faker-fixtures

Generate Faker fixtures for your GraphQL Fragments using GraphQL codegen
TypeScript
4
star
30

docker-python-lint

Flake8 docker
Dockerfile
3
star
31

mach-component-aws-commercetools-token-refresher

Terraform module that runs a Lambda function to auto-refresh commercetools auth tokens in AWS Secrets Manager
Python
3
star
32

terraform-module-commercetools-mc-app

HCL
3
star
33

docker-nginx-s3

Docker container to proxy nginx to s3
Shell
3
star
34

wsgi-aws-unproxy

Process X-Forwarded-For headers for AWS
Python
3
star
35

python-mocksftp

Python
2
star
36

terraform-aws-static-site

AWS static site module for Terraform, with support for basic authentication
HCL
2
star
37

apex-redirector

Go
2
star
38

lambda-utilities-javascript

TypeScript
2
star
39

commercetools-utilities-javascript

TypeScript
2
star
40

commercetools-federated-schema

Library for enabling Apollo GraphQL schema federation for commercetools
TypeScript
2
star
41

terraform-provider-storyblok

Terraform provider to manage Storyblok
Go
2
star
42

graphql-codegen-plugin-amplience

GraphQL codegen plugin for outputting Amplience schema's
TypeScript
2
star
43

node-dynamodb-mock

TypeScript
2
star
44

amplience-go-sdk

Go
2
star
45

go-rds-iam

A SQL Driver for connecting to AWS RDS instances via IAM roles
Go
1
star
46

aws-lambda-deploy

Python
1
star
47

contentstack-go-sdk

Go SDK for the Contentstack management API
Go
1
star
48

django-cache-results

A microlibrary to ease your caching code.
Python
1
star
49

javascript-tools

Tooling for JavaScript projects within Lab Digital
JavaScript
1
star
50

frontend-tooling

๐Ÿ› Tooling and configuration for front-end projects
JavaScript
1
star
51

homebrew-terraform-provider-commercetools

Ruby
1
star
52

tf-ca-proxy

A proxy to use AWS CodeArtifact for hosting Terraform modules
Go
1
star
53

node-federated-token

Tokens for federated services
TypeScript
1
star
54

spaken

Python
1
star
55

js-toolkit

@labdigital/toolkit package for typescript development
TypeScript
1
star
56

next-sentry-tunnel

Sentry tunnel using Next.js route handler
TypeScript
1
star