# hapi-auth-bearer-simple

> Custom authentication plugin for Hapi using Bearer tokens

Latest version **5.0.7** (published 2018-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install hapi-auth-bearer-simple
pnpm add hapi-auth-bearer-simple
yarn add hapi-auth-bearer-simple
bun add hapi-auth-bearer-simple
```

## 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 | 5.0.7 |
| Published | 2018-09-12 |
| First published | 2014-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 2 |
| Unpacked size | 28.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Adri Van Houdt |
| Maintainers | adrivanhoudt |
| Keywords | hapijs, authentication, hapi, plugin, token, auhtorization, simple |

## Links

- npm: https://www.npmjs.com/package/hapi-auth-bearer-simple
- Repository: https://github.com/Salesflare/hapi-auth-bearer-plugin
- Issues: https://github.com/Salesflare/hapi-auth-bearer-plugin/issues
- npm.io page: https://npm.io/package/hapi-auth-bearer-simple

## Dependencies (2)

- [boom](https://npm.io/package/boom.md) 7.x.x
- [hoek](https://npm.io/package/hoek.md) 5.x.x

## 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

- 5.0.7 (latest) — 2018-09-12
- 5.0.6 — 2017-07-10
- 5.0.5 — 2016-12-15
- 5.0.4 — 2016-09-17
- 5.0.2 — 2016-08-17
- 5.0.1 — 2016-08-09
- 5.0.0 — 2016-07-28
- 4.1.0 — 2016-07-06
- 4.0.1 — 2016-03-02
- 4.0.0 — 2016-02-18
- 3.1.0 — 2016-02-12
- 3.0.2 — 2015-11-19
- 3.0.1 — 2015-11-05
- 3.0.0 — 2015-11-02
- 2.0.0 — 2015-10-20
- … 26 more at https://npm.io/package/hapi-auth-bearer-simple/versions

## README

[![Build Status](https://travis-ci.org/Salesflare/hapi-auth-bearer-simple.svg?branch=master)](https://travis-ci.org/Salesflare/hapi-auth-bearer-simple)
[![dep](https://david-dm.org/salesflare/hapi-auth-bearer-simple.svg)](https://david-dm.org/salesflare/hapi-auth-bearer-simple)
[![dev](https://david-dm.org/salesflare/hapi-auth-bearer-simple/dev-status.svg)](https://david-dm.org/salesflare/hapi-auth-bearer-simple?type=dev)
[![peer](https://david-dm.org/salesflare/hapi-auth-bearer-simple/peer-status.svg)](https://david-dm.org/salesflare/hapi-auth-bearer-simple?type=peer)
[![Code Climate](https://codeclimate.com/github/Salesflare/hapi-auth-bearer-simple/badges/gpa.svg)](https://codeclimate.com/github/Salesflare/hapi-auth-bearer-simple)

# Hapi authentication plugin

> [**hapi**](https://github.com/hapijs/hapi) Bearer Token Authentication Scheme

## What

The plugin requires validating a token passed in by the bearer authorization header or via the `access_token` query param. The validation function is something you have to provide to the plugin.

## How

```javascript
var validateFunction = function (token, callback) {

    // Use a real strategy here to check if the token is valid
    if (token === 'abc456789') {
        callback(null, true, userCredentials);
    }
    else {
        callback(null, false, userCredentials);
    }
};

server.register(require('hapi-auth-bearer-simple'), function (err) {

    if (err) {
        throw err;
    }

    server.auth.strategy('bearer', 'bearerAuth', {
        validateFunction: validateFunction
    });

    // Add a standard route here as example
    server.route({
        method: 'GET',
        path: '/',
        handler: function (request, reply) {

            reply({ success: true });
        },
        config: {
            auth: {
                strategy: 'bearer',
                scope: 'user' // or [ 'user', 'admin' ]
            }
        }
    });

    server.start(function (err) {

        if (err) {
            throw err;
        }

        server.log([],'Server started at: ' + server.info.uri);
    });
});
```

- `validateFunction` - (required) a token lookup and validation function with the signature `function (token, callback)`
    - `token` - the auth token received from the client.
    - `callback` - a callback function with the signature `function (err, isValid, credentials)` where:
        - `err` - any error.
        - `isValid` - `true` if both the username was found and the password matched, otherwise `false`.
        - `credentials` - an object passed back to the plugin and which will become available in the `request`object as `request.auth.credentials`. Normally credentials are only included when `isValid`is `true`.
- `exposeRequest` - (optional / advanced) If set to `true` the `validateFunction`'s `this` will be set to the `request`. This can be usefull if you have plugins that expose certain functions/objects on the `request` object and you want to use them in your `validateFunction`.

## Notes

- 100% code coverage!
- You can chain strategies see [](http://hapijs.com/api#serverauthschemename-scheme).
- If you have any problems and/or questions make a new [**issue**](https://github.com/Salesflare/hapi-auth-bearer-simple/issues).
- If you want to contribute feel free to fork and add a pull request or again make an [**issue**](https://github.com/Salesflare/hapi-auth-bearer-simple/issues).

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