Last active
January 13, 2022 06:55
-
-
Save arb/9a1d8e694bbd12d5b455 to your computer and use it in GitHub Desktop.
Example hapi server using GitHub OAuth
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 Hapi = require('hapi'); | |
var Bell = require('bell'); | |
var AuthCookie = require('hapi-auth-cookie'); | |
var server = new Hapi.Server(); | |
server.connection({ port: 9001 }); | |
server.register([Bell, AuthCookie], function (err) { | |
if (err) { | |
console.error(err); | |
return process.exit(1); | |
} | |
var authCookieOptions = { | |
password: 'cookie-encryption-password', //Password used for encryption | |
cookie: 'sitepoint-auth', // Name of cookie to set | |
isSecure: false | |
}; | |
server.auth.strategy('site-point-cookie', 'cookie', authCookieOptions); | |
var bellAuthOptions = { | |
provider: 'github', | |
password: 'github-encryption-password', //Password used for encryption | |
clientId: 'xxxxxxxx',//'YourAppId', | |
clientSecret: 'xxxxxxxx',//'YourAppSecret', | |
isSecure: false | |
}; | |
server.auth.strategy('github-oauth', 'bell', bellAuthOptions); | |
server.auth.default('site-point-cookie'); | |
server.route([ | |
{ | |
method: 'GET', | |
path: '/', | |
config: { | |
auth: { | |
mode: 'optional' | |
}, | |
handler: function (request, reply) { | |
if (request.auth.isAuthenticated) { | |
return reply('welcome back ' + request.auth.credentials.profile.displayName); | |
} | |
reply('hello stranger!'); | |
} | |
} | |
}, { | |
method: 'GET', | |
path: '/account', | |
config: { | |
handler: function (request, reply) { | |
reply(request.auth.credentials.profile); | |
} | |
} | |
}, { | |
method: 'GET', | |
path: '/login', | |
config: { | |
auth: 'github-oauth', | |
handler: function (request, reply) { | |
if (request.auth.isAuthenticated) { | |
request.auth.session.set(request.auth.credentials); | |
return reply('Hello ' + request.auth.credentials.profile.displayName); | |
} | |
reply('Not logged in...').code(401); | |
} | |
} | |
}, { | |
method: 'GET', | |
path: '/logout', | |
config: { | |
auth: false, | |
handler: function (request, reply) { | |
request.auth.session.clear(); | |
reply.redirect('/'); | |
} | |
} | |
} | |
]); | |
server.start(function (err) { | |
if (err) { | |
console.error(err); | |
return process.exit(1); | |
} | |
console.log('Server started at %s', server.info.uri); | |
}); | |
}); |
Afte http://localhost:9001/login in url, getting
{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"}
Me too :
Afte http://localhost:9001/login in url, getting
{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"}
You must enter a password longer than 32 characters for your password in var bellAuthOptions :)
Well, you guys should replace all request.auth.session
with request.cookieAuth
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All references to
request.auth.session
was throwing aTypeError
, if I change it torequest.authCookie
it worked. Not sure why this code as-is didn't work :S