hapi-passport

0.0.6 • Public • Published

hapi-passport

hapi-passport is supposed to be a connector between passport.js strategies and the hapi request api. Right now its in a early phase so its only tested with facebook.

usage

You need to install hapi-passport together with the connector strategies like passport-facebook.

$ npm install hapi-passport passport-facebook

With this you can make a request handler like this:

var FacebookStrategy = require("passport-facebook"),
    facebookLogin = require("hapi-passport")(new FacebookStrategy(...));

and connect it hapi using

server.routes({method: "GET", path: "/login/facebook", facebookLogin() });

By default it would just show loose error or success messages. You can work around that by passing redirect urls for the different cases:

server.routes({
    method: "GET",
    path: "/login/facebook",
    handler: facebookLogin({
        successRedirect: "http://mydomain/login/success",
        errorRedirect: "http://mydomain/login/error",
        failRedirect: "http://mydomain/login/failed"
    })
});

... or you can use handlers for the various cases:

server.routes({
    method: "GET", path: "/login/facebook", handler: facebookLogin({
        onSuccess: function (info, request, reply) {
            // maybe do a redirect?
        },
        onFailed: function (warning, request, reply) {
            // maybe show an error?
        },
        onError: function (error, request, reply) {
            // tell the world that you are angry.
        }
    })
});

Resulting into something like:

var FacebookStrategy = require("passport-facebook"),
    facebookLogin = require("hapi-passport")(new FacebookStrategy({
        clientID: "FACEBOOK_APP_ID", // Facebook app id
        clientSecret: "FACEBOOK_APP_SECRET", // Facebook secret
        callbackURL: "http://localhost:3000/login/facebook" // needs to be added in the facebook admin interface
    }, function verify(accessToken, refreshToken, profile, verified) {
        verified(error, info);
    }));
 
server.routes({
    method: "GET", path: "/login/facebook", handler: facebookLogin({
        onSuccess: function (info, request, reply) {
            // maybe do a redirect?
        },
        onFailed: function (warning, request, reply) {
            // maybe show an error?
        },
        onError: function (error, request, reply) {
            // tell the world that you are angry.
        }
    })
});

Package Sidebar

Install

npm i hapi-passport

Weekly Downloads

112

Version

0.0.6

License

ISC

Last publish

Collaborators

  • leichtgewicht
  • atuyl
  • achingbrain