Last active
July 27, 2016 09:11
-
-
Save sahat/b75c50872a716d3ad25466888b09abdf to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"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", | |
} | |
} |
This file contains 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
// 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. | |
This file contains 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
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