-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
78 lines (67 loc) · 2.2 KB
/
index.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
const http = require('http')
const https = require('https')
const url = require('url')
const port = 4001
const GITHUB_CLIENT = process.env.GITHUB_CLIENT
const GITHUB_SECRET = process.env.GITHUB_SECRET
function httpsRequest(params) {
return new Promise( (resolve, reject) => {
var req = https.get(params, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
console.log(res.url)
return reject(new Error( res.statusCode));
}
let data = ""
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Promise end')
resolve(data)
});
})
req.on('error', function(err) {
reject(err)
})
req.end()
});
}
http.createServer( (req, res) => {
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
'Access-Control-Max-Age': 2592000, // 30 days
'Content-Type': 'application/json;charset=utf-8'
};
if (req.url.match(/github/) ){
const urlQuery = url.parse(req.url, true).query
if ( typeof(urlQuery.code) != "undefined" && typeof(urlQuery.state) != "undefined"){
const options = {
hostname: 'github.com',
path: "/login/oauth/access_token?client_id=" + GITHUB_CLIENT + "&client_secret=" + GITHUB_SECRET + "&code=" + urlQuery.code + "&state=" + urlQuery.state,
headers: {
Accept: 'application/json'
}
}
httpsRequest(options).then( async (data) => {
console.log("data sent")
console.log(data)
res.writeHead(200, headers)
await res.end(data)
}).catch( (error) => {
console.log(error)
res.writeHead(404, headers)
res.end(`{"error": "Request to github server failed"}`)
})
}else{
res.writeHead(405, headers)
res.end(`{"error": "${http.STATUS_CODES[405]}"}`)
}
}else {
// End all the other connections
res.writeHead(405, headers)
res.end(`{"error": "${http.STATUS_CODES[405]}"}`)
}
}).listen(port, () => {
console.log(`Server Listening on port: ${port}`);
})