forked from silbo/codesnail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
195 lines (172 loc) · 7.38 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/env node
/* Add modules */
var express = require('express'),
expressValidator = require('express-validator'),
app = express(),
jade = require('jade'),
flash = require('connect-flash'),
http = require('http'),
passport = require('passport'),
auth = require("./config/authentication"),
db = require("./config/database"),
MongoStore = require('connect-mongo')(express),
config = require("./config/config"),
email = require("./config/email"),
routes = require('./routes'),
user = require('./routes/user');
var SessionStore = new MongoStore({ url: config.database_url });
/* Set app properties */
app.set('view engine', 'jade');
app.use(express.static(__dirname + "/public"));
app.use(express.json());
app.use(express.multipart());
app.use(express.urlencoded());
app.use(expressValidator());
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: "super-secret-u-will-never-guess",
store: SessionStore
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}
app.get("/login", user.login);
app.all("/register", user.register);
app.get("/register/:id", user.verify);
app.all("/forgot", user.forgotPassword);
app.get("/study", routes.study);
app.get("/coding", routes.coding);
app.post("/login", passport.authenticate("local", { successRedirect: "/profile", failureRedirect: "/login", failureFlash: true }));
app.get(config.google.auth, passport.authenticate("google", { scope: config.google.gdata_scopes }));
app.get(config.google.callback, passport.authenticate("google", { successRedirect: "/profile", failureRedirect: "/login", failureFlash: true }));
app.get(config.twitter.auth, passport.authenticate("twitter"));
app.get(config.twitter.callback, passport.authenticate("twitter", { successRedirect: "/profile", failureRedirect: "/login", failureFlash: true }));
/* Facebook Oauth2 bug appends #_=_ to the callback URL */
app.get(config.facebook.auth, passport.authenticate("facebook", { scope: ['email'] }));
app.get(config.facebook.callback, passport.authenticate("facebook", { successRedirect: "/profile", failureRedirect: "/login", failureFlash: true }));
app.get(config.linkedin.auth, passport.authenticate("linkedin", { scope: ['r_basicprofile', 'r_emailaddress'] }));
app.get(config.linkedin.callback, passport.authenticate("linkedin", { successRedirect: "/profile", failureRedirect: "/login", failureFlash: true }));
app.get(config.github.auth, passport.authenticate("github"));
app.get(config.github.callback, passport.authenticate("github", { successRedirect: "/profile", failureRedirect: "/login", failureFlash: true }));
app.get("/logout", user.logout);
/* Homepage */
app.get("/", auth.ensureAuthenticated, routes.index);
/* Profile page */
app.get("/profile", auth.ensureAuthenticated, user.profile);
app.post("/profile/update", auth.ensureAuthenticated, user.profileUpdate);
app.post("/profile/password", auth.ensureAuthenticated, user.passwordUpdate);
app.get("/profile/mugshot/:provider", auth.ensureAuthenticated, user.mugshotUpdate);
app.get("/profile/remove/:name", auth.ensureAuthenticated, user.providerRemove);
/* Delete all the users and providers */
if (false) {
db.User.remove(function (err, removed) {
if (err) console.log("ERROR", "deleting all users:", err);
else console.log("INFO", "successfully removed all users");
});
db.Provider.remove(function (err, removed) {
if (err) console.log("ERROR", "deleting all providers:", err);
else console.log("INFO", "successfully removed all providers");
});
}
/* Show all the users and providers */
db.User.find(function (err, users) {
if (err) console.log("ERROR", "fetching all users:", err);
else users.forEach(function(user) {
console.log("INFO", "user name:", user.name);
});
});
db.Provider.find(function (err, providers) {
if (err) console.log("ERROR", "fetching all providers:", err);
else providers.forEach(function(provider) {
console.log("INFO", "provider url:", provider.url);
});
});
/* Start the app */
var server = http.createServer(app).listen(config.port);
console.log("INFO", "express server listening on port:", config.port);
/* Setup socket sessionstore */
var io = require('socket.io').listen(server);
var passportSocketIo = require('passport.socketio');
io.set('authorization', passportSocketIo.authorize({
cookieParser: express.cookieParser,
key: 'connect.sid',
secret: 'super-secret-u-will-never-guess',
store: SessionStore,
fail: function(data, accept) {
console.log("ERROR", "scoket data:", data);
accept(null, false);
},
success: function(data, accept) {
//console.log("INFO", "scoket:", data);
accept(null, true);
}
}));
/* Online users */
var onlineUsers = {};
var currentTask = 0;
var task = [
"Task 1: Create a basic html structure",
"Task 2: Add a header 1 with text 'Awesome' inside the body",
"Task 3: Add two paragraph under the header, the fist with text 'Cool' and second with text 'Interesting'",
"Task 4: Add a link with text 'My Website' and link 'http://www.mywebsite.com', after the paragraphs"];
var taskVerify = [
'<html(.*)><head>(.*)</head><body></body></html>',
'<html(.*)><head>(.*)</head><body><h1>Awesome</h1></body></html>',
'<html(.*)><head>(.*)</head><body><h1>Awesome</h1><p>Cool</p><p>Interesting</p></body></html>',
'<html(.*)><head>(.*)</head><body><h1>Awesome</h1><p>Cool</p><p>Interesting</p><ahref="http://www.mywebsite.com">MyWebsite</a></body></html>'];
/* User initiated socket connection */
io.sockets.on('connection', function (socket) {
/* Add user to online users */
console.log("INFO", "socket connection established");
console.log("INFO", "socket user:", socket.handshake.user.email);
onlineUsers[socket.handshake.user.email] = {
name: socket.handshake.user.name,
email: socket.handshake.user.email,
profile: {
mugshot: socket.handshake.user.profile.mugshot,
website: socket.handshake.user.profile.website,
description: socket.handshake.user.profile.description
}
};
/* Update the online users for all users */
io.sockets.emit("users", onlineUsers);
/* User asks for someones code */
socket.on('get-code', function(userEmail) {
console.log("INFO", "get user code:", userEmail);
socket.emit("receive-code", onlineUsers[userEmail].code);
});
/* User asks for a task */
socket.on('get-task', function() {
console.log("INFO", "get task:", socket.handshake.user.email);
socket.emit("receive-task", task[currentTask]);
});
/* User verifies a task */
socket.on('verify-task', function(code) {
console.log("INFO", "verifiying task:", code.replace(/\s+/g, ''));
/* Save the users code */
onlineUsers[socket.handshake.user.email].code = code;
if (code.replace(/\s+/g, '').match(taskVerify[currentTask])) {
io.sockets.emit("receive-task-verification", socket.handshake.user.name + " Wins!");
if (currentTask < 3) currentTask += 1;
else currentTask = 0;
io.sockets.emit("receive-task", task[currentTask]);
}
else
io.sockets.emit("receive-task-verification", "");
});
/* User disconnected from socket */
socket.on('disconnect', function() {
console.log("INFO", "socket user disconnected:", socket.handshake.user.email);
/* Delete user from online users */
delete onlineUsers[socket.handshake.user.email];
/* Update the online users for all users */
io.sockets.emit("users", onlineUsers);
});
});