# micro-jwt-auth

> jwt authencication wrapper for zeit/micro

Latest version **1.10.0** (published 2018-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install micro-jwt-auth
pnpm add micro-jwt-auth
yarn add micro-jwt-auth
bun add micro-jwt-auth
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.10.0 |
| Published | 2018-06-22 |
| First published | 2017-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 75.3 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 94 |
| Author | jaga santagostino |
| Maintainers | kandros |

## Links

- npm: https://www.npmjs.com/package/micro-jwt-auth
- Repository: https://github.com/kandros/micro-jwt-auth
- Homepage: https://github.com/kandros/micro-jwt-auth#readme
- Issues: https://github.com/kandros/micro-jwt-auth/issues
- npm.io page: https://npm.io/package/micro-jwt-auth

## Dependencies (1)

- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) 8.3.0

## Recent versions

- 1.10.0 (latest) — 2018-06-22
- 1.9.0 — 2017-08-15
- 1.8.0 — 2017-08-02
- 1.7.0 — 2017-07-02
- 1.6.0 — 2017-04-30
- 1.5.0 — 2017-02-21
- 1.4.0 — 2017-02-19
- 1.3.5 — 2017-02-15
- 1.3.4 — 2017-02-15
- 1.3.3 — 2017-02-15
- 1.3.2 — 2017-02-15
- 1.3.1 — 2017-02-15
- 1.1.0 — 2017-02-15
- 1.0.0 — 2017-02-15

## README

[![Build Status](https://travis-ci.org/kandros/micro-jwt-auth.svg?branch=master)](https://travis-ci.org/kandros/micro-jwt-auth)
[![npm](https://img.shields.io/npm/v/micro-jwt-auth.svg)](https://www.npmjs.com/package/micro-jwt-auth)
# micro-jwt-auth
[json web token(jwt)](https://jwt.io/introduction/) authorization wrapper for [Micro](https://github.com/zeit/micro)

> An `Authorization` header with value `Bearer MY_TOKEN_HERE` is expected

## examples

#### with no other wrappers
```javascript
'use strict'

const jwtAuth = require('micro-jwt-auth')

/*
    if Authorization Bearer is not present or not valid, return 401
*/

module.exports = jwtAuth('my_jwt_secret')(async(req, res) => {
  return `Ciaone ${req.jwt.username}!`
})
```

#### with multiple wrappers

```javascript
'use strict'

const jwtAuth = require('micro-jwt-auth')

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)))

const handle = async(req, res) => {
  return `Ciaone ${req.jwt.username}!`
}

module.exports = compose(
    jwtAuth(process.env.jwt_secret),
    anotherWrapper,
    analitycsWrapper,
    redirectWrapper,
    yetAnotherWrapper
)(handle)
```

#### with whitelist of paths
Whitelisted paths make JWT token *optional*. However if valid token is provided it will be  decoded.

```javascript
'use strict'

const jwtAuth = require('micro-jwt-auth')

/*
    Bypass authentication for login route
*/

module.exports = jwtAuth('my_jwt_secret', [ 'api/login' ])(async(req, res) => {
  return `Ciaone ${req.jwt.username}!`
})
```

#### with custom responses

```javascript
'use strict'

const jwtAuth = require('micro-jwt-auth')

/*
    You can overwrite the default response with the optional config object
*/

module.exports = jwtAuth('my_jwt_secret', ['api/login'], {
  resAuthInvalid: 'Error: Invalid authentication token',
  resAuthMissing: 'Error: Missing authentication token'
})(async(req, res) => {
  return `Ciaone ${req.jwt.username}!`
})

/*
  You may skip the whitelist if unnecessary
*/

module.exports = jwtAuth('my_jwt_secret', {
  resAuthInvalid: 'Error: Invalid authentication token',
  resAuthMissing: 'Error: Missing authentication token'
})(async(req, res) => {
  return `Ciaone ${req.jwt.username}!`
})
```

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