-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (109 loc) · 3.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Module dependencies
*/
var express = require('express'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
routes = require('./routes'),
api = require('./routes/api'),
index = require('./routes/index'),
http = require('http'),
path = require('path'),
auth = require('./routes/auth'),
passwordHash = require('password-hash'),
request = require('request');
var app = module.exports = express();
/**
* Configuration
*/
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.session({secret: 'keyboard cat'}));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
/**
* Middleware for ensuring a user is authenticated before
* allowing him/her to be routed to an authenticated destination.
* @param req request
* @param res response
* @param next the next function
*/
function ensureAuth(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (id , done) {
auth.findUserById(id, function (err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy(
function (username, password, done) {
auth.findByUsername (username, function (err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Unknown user ' + username });
}
if (!passwordHash.verify(password, user.password)) {
return done(null, false, { message: 'Wrong password' });
}
return done(null, user);
});
}
));
// development only
if (app.get('env') === 'development') {
app.use(express.errorHandler());
}
// production only
if (app.get('env') === 'production') {
console.log("Prod baby!");
}
/**
* Routes
*/
// serve index and login and stuff
app.get('/home', index.home);
app.get('/login', index.login);
app.get('/register', index.register);
app.post('/login', passport.authenticate('local', {successRedirect: '/app', failureRedirect: '/login', failureFlash: false}));
app.post('/logout', function (req, res) {
req.logout();
res.redirect('/home');
});
app.post('/register', auth.addUser);
// App routes
app.get('/app', ensureAuth, index.app);
app.get('/session/:id', ensureAuth, index.app);
// JSON API
app.get('/api/sessions', ensureAuth, api.getSessions);
app.post('/api/sessions', ensureAuth, api.addSession);
app.get('/api/sessions/:id', ensureAuth, api.getSessionById);
app.get('/api/participants', ensureAuth, api.getParticipantsBySession);
app.post('/api/participants', ensureAuth, api.addParticipantToSession);
app.del('/api/participants', ensureAuth, api.removeParticipantFromSession);
app.get('/api/users/:id', ensureAuth, api.getUserById);
// redirect all others to the index (HTML5 history)
app.get('*', index.home);
/**
* Start Server
*/
var server = http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(server);
io.set('log level', 1);
io.sockets.on('connection', function(socket) {
auth.userCount(socket);
});