# storyblok-preview-auth

> Connect/Express middleware to securely validate if the user is in edit mode

Latest version **0.1.5** (published 2018-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install storyblok-preview-auth
pnpm add storyblok-preview-auth
yarn add storyblok-preview-auth
bun add storyblok-preview-auth
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2018-11-20 |
| First published | 2018-11-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10 |
| Dependencies | 1 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Heiko Pfahl |
| Maintainers | hpfahl |
| Keywords | storyblok, authentication, express, connect, middleware |

## Links

- npm: https://www.npmjs.com/package/storyblok-preview-auth
- Repository: https://github.com/hpfahl/storyblok-preview-auth
- Homepage: https://github.com/hpfahl/storyblok-preview-auth#readme
- Issues: https://github.com/hpfahl/storyblok-preview-auth/issues
- npm.io page: https://npm.io/package/storyblok-preview-auth

## Dependencies (1)

- [object-assign](https://npm.io/package/object-assign.md) ^4.1.1

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.1.5 (latest) — 2018-11-20
- 0.1.4 — 2018-11-14
- 0.1.3 — 2018-11-13
- 0.1.2 — 2018-11-13
- 0.1.1 — 2018-11-13
- 0.1.0 — 2018-11-13

## README

# storyblok-preview-auth
 storyblok-preview-auth is a node.js package for providing a [Connect](http://www.senchalabs.org/connect/)/[Express](http://expressjs.com/) middleware to securely validate if the user is in [Storyblok](https://www.storyblok.com) edit mode

[![NPM Version](https://img.shields.io/npm/v/storyblok-preview-auth.svg)](https://www.npmjs.com/package/storyblok-preview-auth) [![NPM Downloads](https://img.shields.io/npm/dt/storyblok-preview-auth.svg)](https://www.npmjs.com/package/storyblok-preview-auth) [![GitHub license](https://img.shields.io/github/license/hpfahl/storyblok-preview-auth.svg)](https://github.com/hpfahl/storyblok-preview-auth/blob/master/LICENSE)

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install`](https://docs.npmjs.com/getting-started/installing-npm-packages-locally) command:

```sh
$ npm install storyblok-preview-auth
```

## API

```javascript
var previewAuthenticator = require('storyblok-preview-auth')
```

### previewAuthenticator(options)

Create a previewAuthenticator middleware with the given `options`.

The user is authenticated based on `_storyblok_tk[token]` url parameter as described [here](https://www.storyblok.com/docs/Guides/storyblok-latest-js#how-to-validate-if-the-user-is-viewing-your-site-in-the-storyblo).
If authentication fails, previewAuthenticator middleware will respond with a 401 Unauthorized status, and any additional route handlers will not be invoked. If authentication succeeds, the next handler will be invoked and the _storyblok_tk property will be set in `req.session` to authenticate subsequent requests.

#### Options

`storyblok-preview-auth` accepts these properties in the options object.

##### previewToken

Your preview token. This token allows you to access the draft and published version of your content and can be generated in the Storyblok dashboard at app.storyblok.com.

##### maxAge

A number representing the seconds from `Date.now()` for expiry. previewAuthenticator middleware will respond with a 401 Unauthorized status if timestamp provided as `_storyblok_tk[timestamp]` query parameter is older than maxAge. (1 hour by default)

## Usage
A simple example using `storyblok-preview-auth` to securely validate if the user is in edit mode.

```javascript
var express = require('express')
var session = require('cookie-session')
var previewAuthenticator = require('storyblok-preview-auth')

var app = express()

app.use(session())

app.use(previewAuthenticator({
  previewToken: 'YOUR_PREVIEW_TOKEN',
  maxAge: 1 * 60 * 60 // 1 hour
}))

app.use(...) // Will be invoked only if in edit mode

app.listen(3000);
```

## Todo

- [ ] Testing
- [ ] more more more?

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/storyblok-preview-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
