Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firstandthird/hapi-strategy-loader
Load multiple hapi strategies from json
https://github.com/firstandthird/hapi-strategy-loader
hapi-plugin
Last synced: 14 days ago
JSON representation
Load multiple hapi strategies from json
- Host: GitHub
- URL: https://github.com/firstandthird/hapi-strategy-loader
- Owner: firstandthird
- Created: 2016-05-24T22:52:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-28T20:49:49.000Z (about 7 years ago)
- Last Synced: 2024-08-11T11:33:36.165Z (6 months ago)
- Topics: hapi-plugin
- Language: JavaScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# hapi-strategy-loader [![Build Status](https://travis-ci.org/firstandthird/hapi-strategy-loader.svg?branch=master)](https://travis-ci.org/firstandthird/hapi-strategy-loader)
### A [Hapi server](http://hapijs.com/) plugin to configure your authentication strategies
## Usage:
To use hapi-strategy-loader, you will need to have at least one [authentication strategy](http://hapijs.com/tutorials/auth) registered with your hapi server. This example will use [hapi-auth-cookie](https://github.com/hapijs/hapi-auth-cookie) to manage session authentication.
### Install
At the command line do:> npm install hapi-strategy-loader
### Set up your server and register an authentication strategy:
```js
const Hapi = require('hapi');
const hapiCookie = require('hapi-auth-cookie');const server = new Hapi.Server({ });
server.connection({ port: 8080 });
// register your authentication strategies with hapi
// don't worry about the config options yet:
server.register(hapiCookie, () => {
});
```### Register the hapi-strategy-loader plugin with config options for your strategies:
```js
// these are options recognized by hapi-auth-cookie:
const hapiCookieOptions = {
cookie: 'some-cookie-identifier',
ttl: 60 * 1000,
domain: 'example.com',
};
server.register({
register: require('hapi-strategy-loader'),
// pass any options to hapi-strategy-loader here:
options: {
// set verbose to true to print hapi-strategy-loader debug info:
verbose: true,
// list the strategies to load:
strategies: {
session: {
// the 'cookie' scheme was made available when we registered hapi-auth-cookie:
scheme: 'cookie',
mode: 'try',
// pass the options recognized by hapi-auth-cookie,
// hapi-strategy-loader does not read them
options: hapiCookieOptions
}
}
}
});
```### You can now use the new auth scheme in your server's routes:
```js
// this route will return 401 if the user's session isn't authenticated:
server.route({
method: 'GET',
path: '/',
config: {
auth: 'session',
handler: (request, reply) => {
reply('hello!');
}
}
});
// you can use this route to authenticate the user's session:
server.route({
method: 'GET', path: '/login/{user}',
config: {
auth: { mode: 'try' },
handler: (request, reply) => {
// cookieAuth was provided by hapi-auth-cookie:
request.cookieAuth.set({ user: request.params.user });
return reply(request.params.user);
}
}
});
```See the __test/__ folder for more working examples.