-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (146 loc) · 5.17 KB
/
server.js
File metadata and controls
172 lines (146 loc) · 5.17 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
170
171
172
//express is for routing
var express = require('express');
var path = require('path');
var logger = require('morgan');
//cookieparser is for cookieParser, also for sessions. bodyparser is for accecssing things in the INPUT field
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
//getting passport stuff before accessing the database
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
//right after we connect to mongoose, we connect to the database
//the next two lines represent just that
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI);
//require the authentication backend
var auth = require("./routes/auth");
//require socketio
import socketIO from 'socket.io';
import http from 'http';
import socketApi from './routes/socket';
//grab our mongoose models
var User = require('./models/models').User;
var Document = require('./models/models').Document;
var app = express();
const server = http.Server(app);
const io = socketIO(server);
socketApi(io);
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
//first thing we have on our backend is the passport setup.
//it uses localstrategy, then serialize user, then deserialize user
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// passport strategy
passport.use(new LocalStrategy(function(username, password, done) {
// Find the user with the given username
console.log('hi');
User.findOne({ username: username }, function (err, user) {
// if there's an error, finish trying to authenticate (auth failed)
if (err) {
console.log(err);
return done(err);
}
// if no user present, auth failed
if (!user) {
console.log(user);
return done(null, false, { message: 'Incorrect username.' });
}
// if passwords do not match, auth failed
if (user.password !== password) {
return done(null, false, { message: 'Incorrect password.' });
}
// auth has has succeeded
return done(null, user);
});
}
));
//after deserializing our user, we use auth.js file.
//usually after app.use comes function(), but in this case, that function is auth(passport)
//whenever we do module.exports=function(passport), that's filename(input)
//so in this case, it's auth(passport). auth is the file name and passport is the input
app.use(auth(passport))
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
//BACKEND ROUTES START HERE!!!!!
// Create new document
app.post('/create', function(req, res) {
new Document({
documentName: req.body.documentName, //in the form's input, need to put its name as documentName
owner: req.user, //req.user is produced from the passport when it deserializes
content: '', //content is empty when it starts
collaborators: [req.user], //yourself is automatically inserted as a collaborator when you start a document
password: req.body.password //in the form's input, need to put its name as password
}).save(function(err, doc) {
if (err) {
console.log(err);
res.status(500).json({err: err.message});
return;
}
res.status(200).json({success: true, doc: doc});
})
})
// this is going to GET all the document where the user is a collaborator
app.get('/documents', function(req, res) {
Document.find({collaborators: {$in: [req.user]}}, (err, documents) => {
if (err) res.status(500).end(err.message)
else res.json(documents)
});
});
app.get('/joindocument', function(req, res) {
Document.findById(req.query.id, (err, doc) => {
if (err) res.status(500).end(err.message)
else {
doc.collaborators.push(req.user)}
doc.save(err => {
Document.find({collaborators: {$in: [req.user]}}, (err, documents) => {
if (err) res.status(500).end(err.message)
else res.json(documents)
});
})
})
});
//$in is when you have collabortors, if this user is in this array of collaborators, then return it
// GET request for individual document from documents list (by doc:id)
app.get('/document/:id', function(req, res) {
Document.findById(req.params.id, (err, doc) => {
if (err) res.status(500).end(err.message)
else res.json(doc)
});
});
// saving and editing the document's content
//how does it get req.body.id?????
app.post('/save', function(req, res){
Document.update({ _id: req.body.id }, { $set: { content: req.body.newContent }}, (err, result) => {
if (err) {
res.json({success: false});
} else res.json({success: true})
})
});
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
server.listen(process.env.PORT || 3000)
module.exports = app;