Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (139 loc) · 3.04 KB
/
Copy pathindex.js
File metadata and controls
170 lines (139 loc) · 3.04 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Module dependencies
*/
var passport = require('passport')
, config = require('../config')
, models = require('./models')
, crypto = require('crypto')
, debug = require('debug')('Balloons:routes');
/**
* Get models
*/
var Room = models('Room');
/**
* Set routes
*/
module.exports = function(app, db) {
app.get( '/', isAuth, redirect('/rooms'));
app.get( '/logout', logout, redirect('/'));
app.get( '/rooms', isAuth, loadPublicGroups, render('room_list'));
app.post( '/create', isAuth, validateRoom, createRoom);
app.post( '/:room_key', isAuth, loadRoom, loadRoomUsers, loadRooms, getStatus, render('room'));
};
/**
* check if user is auth
* otherwise show homepage
*/
var isAuth = function(req, res, next) {
(req.isAuthenticated()) ?
next() : res.render('index');
};
/**
* Render a specific path
*/
var render = function(path) {
return function(req, res) {
res.render(path);
};
};
/**
* Redirect to specific uri
*/
var redirect = function(path) {
return function(req, res) {
res.redirect(path);
};
};
/**
* Logout
*/
var logout = function(req, res, next) {
req.logout();
next();
};
/**
* Load public rooms
*/
var loadPublicRooms = function(db) {
return function(req, res, next) {
Room.getPublic(db, function(err, rooms){
if(err) throw err;
res.locals.rooms = rooms;
next();
});
};
};
/**
* Validate room
*/
var validateRoom = function(req, res, next) {
var room_name = encodeURIComponent(req.body.room_name.trim());
if(validRoomName(room_name)) {
Room.findById(db, room_name, function(err, room){
if(err) throw err;
if(!room) {
req.room_name = roo
75EF
m_name;
next();
} else {
// TODO: flash message
res.redirect('back');
}
});
} else {
// TODO: flash message
res.redirect('back');
}
};
/**
* Validate room name
*/
var validRoomName = function(str) {
return str && str.length < 255;
};
/**
* Create a room and redirect
*/
var createRoom = function(req, res) {
var room = new Room.model({
key: genRoomKey(),
name: req.room_name,
admin: req.user.provider + ":" + req.user.username,
locked: 0,
online: 0
});
room.save(function(err, r){
if(err) throw err;
res.redirect('/' + r.key);
});
};
/**
* Generate a room key
*/
var genRoomKey = function() {
var shasum = crypto.createHash('sha1');
shasum.update(Date.now().toString());
return shasum.digest('hex').substr(0,6);
};
/*
* Authentication routes
*
* TODO: Fix this
*/
if(config.auth.twitter.consumerkey.length) {
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect: '/',
failureRedirect: '/'
})
);
}
if(config.auth.facebook.clientid.length) {
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/'
})
);
}