forked from fokenhelpmy/webcdr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
114 lines (97 loc) · 2.73 KB
/
server.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
112
113
114
'use strict';
var _ = require('lodash');
var express = require('express');
var compress = require('compression');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var path = require('path');
var config = require('./lib/config');
var Users = require('./lib/models/users');
var users = new Users();
var i18n = require('./lib/i18n');
i18n.init();
var locale = require('locale');
var passport = require('passport');
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
users.query('where', 'id', '=', id)
.fetch()
.then(function (col) {
done(null, col.at(0).toJSON());
})
.catch(function (err) {
done(err);
});
});
passport.use(require('./lib/auth')(users));
var app = express();
app.use(morgan('dev')); // logger
app.use(compress());
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cookieSession({
secret: config.session.key
}));
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
app.use(locale(i18n.supported()));
app.use('/login', bodyParser.urlencoded({extended: false}));
app.get('/login', function (req, res) {
res.render('login', {
$$: i18n.getTranslator(req.locale),
urlPrefix: config.web.urlPrefix
});
});
app.post('/login',
passport.authenticate('local', {
successRedirect: config.web.urlPrefix + '/',
failureRedirect: config.web.urlPrefix + '/login'
})
);
app.get('/logout', function (req, res) {
req.logout();
res.redirect(config.web.urlPrefix + '/');
});
app.use('/api', ensureAuthenticated);
app.use('/api', require('./lib/api'));
app.get('/', ensureAuthenticated);
app.get('/', function (req, res) {
res.render('index', {
locale: req.locale,
$$: i18n.getTranslator(req.locale),
urlPrefix: config.web.urlPrefix
});
});
app.get('/profile', ensureAuthenticated);
app.get('/profile', function (req, res) {
res.send(_.omit(req.user, 'password'));
});
app.use('/admin', ensureAdmin);
app.use('/admin', require('./lib/admin')(users));
app.use(express.static(path.join(__dirname, 'public')));
const port = process.env.PORT || 8080;
app.listen(port);
console.log(new Date(), 'Server is now running on port', port);
function ensureAuthenticated (req, res, next) {
if (!req.isAuthenticated()) {
res.redirect(config.web.urlPrefix + '/login');
next('Failed login attempt');
return;
}
next();
}
function ensureAdmin (req, res, next) {
ensureAuthenticated(req, res, function () {
if (!req.user.admin) {
res.json({error: 'access denied'});
} else {
next();
}
});
}