Skip to content

Instantly share code, notes, and snippets.

@sahat
Last active July 27, 2016 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahat/b75c50872a716d3ad25466888b09abdf to your computer and use it in GitHub Desktop.
Save sahat/b75c50872a716d3ad25466888b09abdf to your computer and use it in GitHub Desktop.
{
"name": "hapiapp",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"hapi": "^13.5.0",
"request": "^2.74.0",
}
}
// React routes
// Install react-cookie module
// Inside function ensureAuthenticated check if cookie named "user" is present
// if present do nothing, i.e. component should render
// if not present, redirect to Login component.
// Logout button should clear user cookie using react-cookie's method.
const Hapi = require('hapi');
const request = require('request');
const qs = require('querystring');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
server.state('user', {
encoding: 'base64json'
});
server.route({
method: 'GET',
path:'/',
handler: function (req, reply) {
reply('User state ' + JSON.stringify(req.state.user));
}
});
server.route({
method: 'GET',
path:'/login',
handler: function (req, reply) {
const params = qs.stringify({
client_id: 'YOUR CLIENT ID',
redirect_uri: 'http://localhost:3000/login-complete',
scope: 'email',
state: 'random'
});
// initiate authorization
return reply.redirect('https://github.com/login/oauth/authorize?' + params);
}
});
server.route({
method: 'GET',
path:'/login-complete',
handler: function (req, reply) {
var params = {
code: req.query.code,
client_id: 'YOUR CLIENT ID',
client_secret: 'YOUR CLIENT SECRET',
redirect_uri: 'http://localhost:3000/login-complete'
};
// Step 1. Exchange authorization code for access token.
request.get({ url: 'https://github.com/login/oauth/access_token', qs: params }, function(err, response, accessToken) {
console.log(accessToken);
accessToken = qs.parse(accessToken);
const headers = { 'User-Agent': 'Satellizer' };
// Step 2. Retrieve profile information about the current user.
request.get({ url: 'https://api.github.com/user', qs: accessToken, headers: headers, json: true }, function(err, response, profile) {
if (profile) {
// We now have user profile and access token at this point
// You can either save just the user info or user info + access token here
// It will be stored inside a cookie called "user", which you can verify by opening Chrome Dev Tools and going to Resources > Cookies.
// Cookie will be stored as Base64 encoded string, so you will need to do window.atob() to decode it on the client-side.
return reply.redirect('/').state('user', profile);
}
});
});
}
});
server.start((err) => {
console.log('Server running at:', server.info.uri);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment