-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (134 loc) · 4.32 KB
/
index.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
var passport = require('passport');
var LocalStrategy = require("passport-local").Strategy;
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var app = express();
var sio = require("socket.io");
// security
var crypto = require('crypto');
var jwt = require('jsonwebtoken');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// mongodb database
var connection;
if (process.env.NODE_ENV!='production') {
connection = mongoose.connect('mongodb://localhost/cmimc_db');
} else {
connection = mongoose.connect(process.env.MONGODB_URI);
}
autoIncrement.initialize(connection);
var StaffSchema = new mongoose.Schema({
email: String,
password: String,
name: String,
type: String,
andrewid: String,
salt: String
});
StaffSchema.plugin(autoIncrement.plugin, { model: 'Staff', field: 'staffid' });
mongoose.model('Staff', StaffSchema);
var ProposalSchema = new mongoose.Schema({
staffid: Number,
staffname: String,
topic: String,
problem: String,
answer: String,
solution: String,
difficulty: Number,
checked: Boolean
});
ProposalSchema.plugin(autoIncrement.plugin, { model: 'Proposals', field: 'probid' });
mongoose.model('Proposals', ProposalSchema);
var CommentSchema = new mongoose.Schema({
staffid: Number,
probid: Number,
comment: String
});
mongoose.model('Comments', CommentSchema);
var SolutionSchema = new mongoose.Schema({
staffid: Number,
probid: Number,
solution: String
});
mongoose.model('Solutions', SolutionSchema);
var Staff = mongoose.model('Staff');
var Proposals = mongoose.model('Proposals');
var Comments = mongoose.model('Comments');
var Solutions = mongoose.model('Solutions');
// set up passport
passport.use(new LocalStrategy(
function(username, password, done) {
var email = username;
Staff.find({email: email}, function(err, result) {
if (err) { return done(err); }
if(result.length == 0) {
return done(null, false, { message: 'Incorrect username.' });
}
else {
var submitPassword = result[0].password;
var salt = result[0].salt;
var hash = crypto.pbkdf2Sync(password, salt, 1000, 64).toString('hex');
if (submitPassword !== hash) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, result[0]);
}
});
}));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.use(passport.initialize());
app.use(passport.session());
var routes = require('./routes');
app.use('/', routes);
var server = app.listen(process.env.PORT || 3000, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
// socket.io
var io = sio.listen(server)
io.on('connection', function (socket) {
socket.on('disconnect', function () {
console.log('Client disconnected from server')
})
socket.on('problem proposal', function(proposal) {
console.log('New problem proposal: ' + JSON.stringify(proposal.data))
socket.broadcast.emit('problem proposal', proposal.data)
})
socket.on('comment', function(comment) {
console.log('New comment: ' + JSON.stringify(comment))
socket.broadcast.emit('comment', comment)
})
socket.on('solution', function(solution) {
console.log('New solution: ' + JSON.stringify(solution))
socket.broadcast.emit('solution', solution)
})
socket.on('staff type update', function(update) {
console.log('Staff type update: ' + JSON.stringify(update))
Staff.findOne({staffid: update.staffid}, function (err, staff) {
if (err) {
socket.emit('error', 'cannot find the staff')
return
} else {
update.jwt = routes.generateJWT(staff.name,staff.email,update.type,staff.staffid)
socket.broadcast.emit('staff type update', update)
}
})
})
socket.on('proposal deleted', function(data) {
console.log('Proposal deleted: ' + JSON.stringify(data))
io.emit('proposal deleted', data)
})
console.log('Client socket connected')
})