-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.js
74 lines (55 loc) · 2.05 KB
/
app.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
var express = require('express');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var url = require('url');
var sass = require('node-sass');
var path = require('path');
var app = express();
var cookieParser = require('cookie-parser');
var configurable = require('./lib/configurable');
var _ = require('underscore');
var configServer = require('./config/server.js');
var configClient = require('./config/client.js');
var oauthUrl = configServer.oauthUrl;
var clientId = configServer.clientId;
var clientSecret = configServer.clientSecret;
app.use(sass.middleware({
src: __dirname + '/app',
debug: false
}));
app.use(express.static(path.join(__dirname, 'app')));
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(cookieParser());
app.get('/config', function(req, res) {
res.send(configClient);
});
app.get('/request_code', function(req, res) {
var authorizeUrl = oauthUrl + '/authorize?client_id=' + clientId;
var access = req.query.access || req.cookies.access || 'public_repo';
res.cookie('access', access, { expires: new Date(Date.now() + (365 * 24 * 60 * 60 * 1000)) } );
res.redirect(authorizeUrl + '&scope=' + access);
});
app.get('/request_auth_token', function(req, res) {
var getAuthTokenUrl = oauthUrl + '/access_token?' +
'client_id=' + clientId +
'&client_secret=' + clientSecret +
'&code=' + req.query.code;
var xhr = new XMLHttpRequest();
xhr.open('POST', getAuthTokenUrl, false);
xhr.send();
if(xhr.responseText.indexOf('incorrect_client_credentials') > 0){
console.log('ERROR - Invalid clientID or clientSecret');
}
var fakeUrl = 'http://fake.uri/?' + xhr.responseText;
var repositoryAccess = url.parse(fakeUrl, true).query['scope'];
res.redirect('/?access=' + repositoryAccess + '#' + url.parse(fakeUrl, true).query['access_token']);
});
require('./lib/priorization')(app, {
url: configServer.redisUrl
});
require('./lib/columns')(app, {
url: configServer.redisUrl
});
port = process.env.PORT || 3000;
app.listen(port);
console.log('Server started. Running on port', port);