This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (96 loc) · 2.69 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require('es6-promise').polyfill();
var fetch = require('isomorphic-fetch');
var queryString = require('query-string');
var express = require('express');
var exphbs = require('express-handlebars');
var cookieParser = require('cookie-parser');
var morgan = require('morgan');
var cookieSession = require('cookie-session');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(morgan('combined'));
var oauth_host = 'https://modao.cc';
var host = 'https://modao-oauth-api-demo.herokuapp.com';
app.use(cookieParser(process.env.APP_SECRET));
app.use(cookieSession({
name: '_MBIPA_',
secret: process.env.APP_SECRET,
maxAge: 1000 * 60 * 60 * 24
}))
app.engine('handlebars', exphbs({defaultLayout: 'default'}));
app.set('view engine', 'handlebars');
app.use(express.static('public'));
app.get('/', function (req, res) {
if(req.session['token']) {
fetch(oauth_host + '/api/v1/user_info', {
method: 'get',
headers: {
'Authorization': 'Bearer ' + req.session['token']
}
})
.then(status)
.then(json)
.then(function (user_info) {
res.render('index', {
user_name: user_info.name,
avatar: user_info.avatar
});
})
.catch(function (error) {
console.log('Request failed', error);
res.redirect('/logout');
});
} else {
res.render('index');
}
});
app.get('/login', function (req, res) {
var params = {
client_id: process.env.CLIENT_ID,
redirect_uri: host + '/auth/Mockingbot/callback',
state: 'abcd1234',
response_type: 'code'
};
res.redirect(oauth_host + '/oauth2/v1/authorize?' + queryString.stringify(params));
});
app.get('/logout', function (req, res) {
req.session = null
res.redirect('/');
});
app.get('/auth/Mockingbot/callback', function (req, res) {
var code = req.query.code;
var payload = {
redirect_uri: host + '/auth/Mockingbot/callback',
client_id: process.env.CLIENT_ID,
code: code,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'authorization_code'
};
fetch(oauth_host + '/oauth2/v1/token', {
method: 'post',
body: queryString.stringify(payload)
})
.then(status)
.then(json)
.then(function (data) {
var token = data.access_token;
req.session['token'] = token;
res.redirect('/');
})
.catch(function (error) {
console.log('Request failed', error);
});
});
function status(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
return Promise.reject(new Error(response.statusText))
}
}
function json(response) {
return response.json()
}
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});