-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (53 loc) · 1.55 KB
/
Copy pathindex.js
File metadata and controls
61 lines (53 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const hapi = require('hapi');
const inert = require('inert');
const server = new hapi.Server();
const env = require('env2')('./config.env');
const wreck = require('wreck');
const qs = require('querystring');
server.connection({
port: '3000'
});
server.register(inert, (err) => {
if (err)
throw err
})
server.route({
method: 'GET',
path: '/',
handler: (request, reply) => {
reply.file("./views/index.html");
}
});
server.route({
method: 'GET',
path: '/welcome',
handler: (request, reply) => {
wreck.post('https://github.com/login/oauth/access_token', {
payload: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code: request.url.query.code
}
}, (err, res, payload) => {
const full = qs.parse(payload.toString())
const access_token = full.access_token;
wreck.get('https://api.github.com/user?access_token=' + access_token, {
headers: {'user-agent': 'node.js'}
}, (err, res, payload) => {
console.log(qs.parse(payload.toString()));
});
});
}
});
server.route({
method: 'GET',
path: '/login',
handler: (request, reply) => {
reply.redirect('https://github.com/login/oauth/authorize?client_id=' + process.env.CLIENT_ID + '&redirect_uri=http://localhost:3000/welcome');
}
});
server.start((err) => {
if (err)
throw err;
console.log(`Server running at: ${server.info.uri}`);
});