-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (52 loc) · 1.58 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
/* EXPRESS */
// import express framework
const express = require('express');
const app = express();
// import axios network library for http calls
const axios = require('axios')
app.set('view engine', 'ejs');
var access_token = "";
app.get('/', function(req, res) {
res.render('pages/index',{client_id: clientID});
});
//register route to render failed page
app.get('/failed', function(req, res) {
res.render('pages/failed');
});
//register route to render logout page
app.get('/logout', function(req, res) {
res.render('pages/logout');
});
const port = process.env.PORT || 4000;
app.listen(port , () => console.log('App listening on port ' + port));
const clientID = 'YOUR_GITHUB_CLIENT_ID_HERE'
const clientSecret = 'YOUR_GITHUB_CLIENT_SECRET_HERE'
app.get('/home', (req, res) => {
//if user denies access to github account display login failed page.
if(req.query.error=="access_denied"){
res.redirect('/failed');
return;
}
const requestToken = req.query.code
axios({
method: 'post',
url: `https://github.com/login/oauth/access_token?client_id=${clientID}&client_secret=${clientSecret}&code=${requestToken}`,
headers: {
accept: 'application/json'
}
}).then((response) => {
access_token = response.data.access_token
res.redirect('/success');
})
})
app.get('/success', function(req, res) {
axios({
method: 'get',
url: `https://api.github.com/user`,
headers: {
Authorization: 'token ' + access_token
}
}).then((response) => {
res.render('pages/success',{ userData: response.data });
})
});