This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
app.js
96 lines (76 loc) · 2.54 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var ejs = require('ejs');
var express = require('express');
var session = require('express-session');
var http = require('http');
var methodOverride = require('method-override');
var passport = require('passport');
var GitHubStrategy = require('passport-github2').Strategy;
var api = require('./api/api');
var env = require('./lib/env').load();
var middleware = require('./lib/middleware');
var ensureAuthenticated = middleware.ensureAuthenticated;
var ensureGithubOrg = middleware.ensureGithubOrg;
var addToUsers = middleware.addToUsers;
var includeBranding = middleware.includeBranding;
var server;
var app = express();
var port = process.env.PORT || 3000;
var requiredEnv = ['GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET',
'GITHUB_ORG', 'SESSION_SECRET'];
requiredEnv.forEach(function (name) {
if (!env[name]) {
throw new Error('Missing required environment variable: ' + name);
}
});
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
passport.use(new GitHubStrategy({
clientID: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
callbackURL: env.GITHUB_CALLBACK_URL || 'http://localhost:' + port + '/auth/callback'
}, function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}));
app.engine('html', ejs.renderFile);
app.set('views', process.cwd() + '/views');
app.use(methodOverride());
app.use(session({
secret: env.SESSION_SECRET,
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/private/index.html', [ensureAuthenticated, ensureGithubOrg,
addToUsers, includeBranding],
function (req, res) {
res.render('index.html');
}
);
app.use('/private', [ensureAuthenticated, ensureGithubOrg,
addToUsers, express.static('private')]);
app.use('/api', [ensureAuthenticated, ensureGithubOrg, addToUsers, api.router]);
app.use('/', express.static('public'));
app.get('/auth', passport.authenticate('github', { scope: ['user:email'] }), function () {});
app.get('/auth/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
function (req, res) {
res.redirect('/private/index.html');
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
module.exports = app;
if (require.main === module) {
server = http.createServer(app);
server.listen(port, function () {
console.log('Checklistomania started on port ' + port); // eslint-disable-line no-console
});
}