1.7.0 • Published 2 years ago

@storyofams/next-password-protect v1.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Preview

How it works

This library adds a password prompt to your Next.js deployment. It consists of two main parts: 1. Two serverless API routes:

  • A login route that checks if a password is correct and sets a cookie with a JWT in case it is.
  • A check route that validates if you have the authorization cookie with a valid JWT.
  1. A HOC (Higher-Order Component) that wraps Next.js App and adds a check that validates if you are logged in. If you do, then you can view the app normally; otherwise, you are presented with a password prompt.

Important: The recommended use case for this library is in a staging or preview environment. By taking advantage of webpack's DefinePlugin, we can make sure this library is only included in certain environments, keeping the production bundle size small.

This library is NOT meant as a secure password authentication wrapper, but rather as a way to keep nosey people out.

Installation

yarn add @storyofams/next-password-protect
# or
npm install @storyofams/next-password-protect

Usage

There are 3 steps to enabling password protect: setting a global variable, adding the API routes, and adding the HOC to _app.

Step 1

In order to be able to take advantage of dead code elimination, it is recommended to add an environment variable like process.env.PASSWORD_PROTECT, and enable the library based on that variable. To set this variable, add the following to next.config.js:

module.exports = {
  env: {
    // Add any logic you want here, returning `true` to enable password protect.
    PASSWORD_PROTECT: process.env.ENVIRONMENT === 'staging',
  }
});

Step 2

Add two api routes, one with the loginHandler and one with the passwordCheckHandler api function. You can name them anything, as long as you pass the names to loginApiUrl and checkApiUrl respectively, in the next step. By default it expects /login and /passwordCheck.

import { loginHandler } from "@storyofams/next-password-protect";

export default loginHandler("YOUR_SECRET_PASSWORD", {
  // Options go here (optional)
  cookieName: "next-password-protect",
});
import { passwordCheckHandler } from "@storyofams/next-password-protect";

export default passwordCheckHandler("YOUR_SECRET_PASSWORD", {
  // Options go here (optional)
  cookieName: "next-password-protect",
});

Step 3

Add the withPasswordProtect HOC to the default export of App in pages/_app.tsx:

import { withPasswordProtect } from "@storyofams/next-password-protect";

// Before: export default App;
export default process.env.PASSWORD_PROTECT
  ? withPasswordProtect(App, {
    // Options go here (optional)
    loginApiUrl: "/login",
  })
  : App;

Note: make sure to specify loginApiUrl and/or checkApiUrl if the api route(s) are not default.

API

API routes handlers

loginHandler(password: string, options)

The options object can contain any of the following options:

OptionDescriptionDefault value
cookieMaxAgeCookie Max-Age attributeundefined
cookieNameThe name of the authorization cookie'next-password-protect'
cookieSameSiteSameSite cookie attributefalse
cookieSecureSecure flag on the cookieprocess.env.NODE_ENV === 'production'

passwordCheckHandler(password: string, options)

The options object can contain any of the following options:

OptionDescriptionDefault value
cookieNameThe name of the authorization cookie'next-password-protect'

Next App HOC

withPasswordProtect(App: NextApp, options)

The options object can contain any of the following options:

OptionDescriptionDefault value
checkApiUrlRelative path of the api route handled by passwordCheckHandler'/api/passwordCheck'
loginApiUrlRelative path of the api route handled by loginHandler'/api/login'
loginComponentSupply your own React component to show as login promptLoginComponent
loginComponentPropsProperties object to customize the login prompt, without overriding the entire component (see below){}
bypassProtectionBypass protection for specific routes, decided by callback with NextRouter param({ route }) => false

The loginComponentProps object can contain any of the following options:

OptionDescriptionDefault value
backUrlShow a link with this URL to go back to main websiteundefined
buttonBackgroundColorLogin button background color'#01EDBC'
buttonColorLogin button color'#111'
logoShow a logo above the prompt (img src)undefined

Advanced

Custom login component

To change the default login component, a React component can be supplied to the withPasswordProtect HOC. In order for the library to function properly, make sure your login component has password input that is validated by the the api route. You can use src/hoc/LoginComponent.tsx as a starting point.

Caveats

AMP is not yet supported, because the LoginComponent failed AMP validation. On an AMP page, nothing is rendered. This could be fixed by changing LoginComponent to valid AMP.

1.7.0

2 years ago

1.6.0

2 years ago

1.5.14

2 years ago

1.5.13

2 years ago

1.5.12

3 years ago

1.5.11

3 years ago

1.5.10

3 years ago

1.5.9

3 years ago

1.5.8

3 years ago

1.5.7

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.4.2

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.3.2

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago