forked from msachi/oauth-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (88 loc) · 2.13 KB
/
server.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const Hapi = require('hapi');
const inert = require('inert');
const querystring = require('querystring');
const Request = require('request');
const env = require('env2');
const server = new Hapi.Server();
env('./config.env');
server.connection ({
port: 4000
});
server.register(inert, (err) => {
if (err) throw err;
server.state('GitHubCookie', {
ttl: 60 * 60 * 1000,
isSecure: false,
isHttpOnly: false,
encoding: 'base64json',
clearInvalid: false,
strictHeader: true
});
server.route([{
method: 'GET',
path: '/{file*}',
handler: {
directory: {
path: 'public/'
}
}
},
{
method: 'GET',
path: '/login',
handler: (req, reply) => {
var queryString = querystring.stringify({
client_id: process.env.CLIENT_ID,
redirect_uri: process.env.BASE_URL + '/welcome',
scope: 'user public_repo'
});
reply.redirect('https://github.com/login/oauth/authorize?' + queryString);
}
},
{
method: 'GET',
path: '/welcome',
handler: (req, reply) => {
Request({
url: 'https://github.com/login/oauth/access_token',
method: 'POST',
form: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
code: req.url.query.code
}
}, (err, res, body) => {
if (err) throw err;
reply(body).state('GitHubCookie', {
token: querystring.parse(body).access_token
});
});
}
},
{
method: 'POST',
path: '/send-issue',
handler: (req, reply) => {
Request({
url: 'https://api.github.com/repos/FAC9/READMES/issues',
method: 'POST',
headers: {
Authorization: 'token ' + req.state.GitHubCookie.token,
'User-Agent': 'oauth-workshop'
},
body: JSON.stringify({
title: req.payload.title,
body: req.payload.body
})
}, (err, res) => {
if (err) throw err;
console.log(res.body);
})
}
}
]);
});
server.start( (err) => {
if (err) throw err;
console.log(`server is running on: ${server.info.uri}`);
});