Created
February 7, 2013 18:40
-
-
Save thegoleffect/4733120 to your computer and use it in GitHub Desktop.
early interface for hapi-passport integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var passport = require('passport'); | |
var Hapi = require('hapi'); | |
var util = require('util'); | |
var FacebookStrategy = require('passport-facebook').Strategy | |
var server = new Hapi.Server('localhost', 8000); | |
// Passport | |
passport.use(new FacebookStrategy({ | |
clientID: CLIENTID, | |
clientSecret: SECRET, | |
callbackURL: "http://localhost:8000/auth/facebook/callback" | |
}, function (accessToken, refreshToken, profile, done) { | |
return done(null, profile); | |
})); | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(obj, done) { | |
done(null, obj); | |
}); | |
server.ext('onRequest', [ | |
passport.initialize(), | |
passport.session() | |
]); | |
var ensureAuthenticated = function (request, next) { | |
console.log('isAuthenticated', request.isAuthenticated()) | |
if (request.isAuthenticated()) { | |
return next(); | |
} | |
request.reply.redirect('/login').send(); | |
}; | |
// Handlers | |
var authFB = { | |
handler: passport.authenticate('facebook') | |
}; | |
var authFBCB = { | |
handler: function (request) { | |
passport.authenticate('facebook')(request, function () { | |
// console.log('request._passport', request._passport) | |
// console.log(util.inspect(request), null, 2) | |
request.reply("authenticated with request.user = ", request.user); | |
}) | |
} | |
}; | |
server.addRoute({ | |
method: 'GET', | |
path: '/auth/facebook', | |
config: authFB | |
}); | |
server.addRoute({ | |
method: 'GET', | |
path: '/auth/facebook/callback', | |
config: authFBCB | |
}); | |
server.addRoute({ | |
method: 'GET', | |
path: '/login', | |
config: { | |
handler: function (request) { | |
request.reply('<a href="/auth/facebook">Login with Facebook</a>') | |
} | |
} | |
}) | |
server.addRoute({ | |
method: 'GET', | |
path: '/admin', | |
config: { | |
handler: function (request) { | |
console.log('request.user about to auth:', request.user) | |
ensureAuthenticated(request, function () { | |
request.reply("Access Granted"); | |
}); | |
} | |
} | |
}); | |
server.start(function(){ | |
console.log('server started') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment