@matthewglover/hapi-oauth

Oauth login plugin for Hapi.js


License
MIT
Install
npm install @matthewglover/hapi-oauth@0.1.1

Documentation

Build Status Coverage Status

hapi-oauth

What

A Hapi.js plugin providing Oauth login. Currently just for Facebook.

Why

To implement a simple minimal oauth setup, and to learn more about oauth and plugins.

How

To install, run npm install --save @matthewglover/hapi-oauth.

A simple implementation:

const { createServer, setConnection, registerPlugins, addRoutes, startServer } =
  require('@matthewglover/hapi-wrapper');
const hapiOauth = require('@matthewglover/hapi-oauth');

const options = {
  configs: [
    {
      provider: 'facebook',                       // name of provider (currently only facebook)
      loginPath: '/fb-login',                     // path to start login process
      authPath: '/fb-auth',                       // path to complete login process
      redirectPath: '/process-login',             // path to handle any post-login
      baseUrl: 'your.domain.com',                 // base path of your domain (without)
      clientId: 'your-facebook-client-id',        // facebook client id
      clientSecret: 'your-facebook-client-secret',// facebook client secret
      options: {                                  // any additional params to be sent to provider
        scope: 'user_likes',
      },
    },
  ],
};


// Create plugin with options
const plugin = { register: hapiOauth, options };

// Example route to process data from login
// This is where you may wan to create a web token or cookie
const processLoginRoute = {
  method: 'GET',
  path: '/process-login',
  handler: (req, reply) => reply(req.query),
};


// Create a server, register plugin and add process-login route
const server =
  createServer()
  .then(setConnection({ port: 3000 }))
  .then(registerPlugins([plugin]))
  .then(addRoutes([processLoginRoute]))
  .then(startServer)
  .then(s => console.log(`Server running at: ${s.info.uri}`))
  .catch(err => console.error(err));

module.exports = server;