Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collaborators and searching docs #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 217 additions & 5 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,223 @@
const express = require('express')
const app = express()
const express = require('express');
const app = express();

// Example route
app.get('/', function (req, res) {
res.send('Hello World!')
const path = require('path');
const bodyParser = require('body-parser')
const passport = require('passport');
const LocalStrategy = require('passport-local');
const User = require('../reactApp/models').User;
const Document = require('../reactApp/models').Document;

var session = require('express-session');
const MongoStore = require('connect-mongo')(session);


app.use(express.static(path.join(__dirname, 'build')));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI)

app.use(session({
secret: 'secret',
store: new MongoStore({
mongooseConnection: mongoose.connection
})
}));

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
// May need to adapt this to your own model!
User.findOne({ username: username }, function (err, user) {
// if there's an error, finish trying to authenticate (auth failed)
if (err) { return done(err); }
// if no user present, auth failed
if (!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);
});
}));

app.use(passport.initialize());
app.use(passport.session());

app.post('/login', passport.authenticate('local'), (req, res) => {
//if the passport authenticate doesn't respond, then it goes to (req, res); can have multiple (req, res) until one responds
res.json({success: true, user: req.user});
//passport.authenticate
})

app.post('/register', (req, res) => {
//make a new User and save it
const newUser = new User({
username: req.body.username,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName,
})
newUser.save((err, user) => {
if(err) {
res.json({success: false, error: err})
} else {
res.json({success: true})
}
})
})

app.post('/docs', (req, res) => {
//make a new User and save it
const newDocument = new Document({
title: req.body.title,
ownerid: req.user._id,
owner: req.user.username,
collaborators: req.body.collaborators,
password: req.body.password,
})
newDocument.save((err, newDoc) => {
if(err) {
res.json({success: false, error: err})
} else {
res.json({success: true, doc: newDoc})
}
})
})

app.use((req, res, next) => {
if (req.user) {
next();
} else {
res.redirect('/login')
}
});

app.get('/getDocs', (req, res) => {
// Document.find({ownerid: req.user._id, collaborators: { $in: [req.user.username] }}, (err, docs) => {
Document.find({ $or: [{ownerid: req.user._id}, {collaborators: { $in: [req.user.username] }} ]}, (err, docs) => {
if (err) {
res.send(err)
} else {
res.json(docs)
}
})
})

app.get('/getDoc/:docid', (req, res) => {
Document.findById(req.params.docid, (err, doc) => {
if (err) {
res.send(err)
} else {
res.json(doc)
}
})
})

// app.get('/searchDoc/:docid', (req, res) => {
// Document.findById(req.params.docid, (err, doc) => {
// if (err) {
// res.send(err)
// } else {
// res.json(doc)
// }
// })
// })

app.post('/saveDoc/:docid', (req, res) => {
Document.update({_id: req.params.docid}, { $set: {body: req.body.body, inlineStyles: req.body.inlineStyles} }, (err, result) => {
if (err) {
res.send({ success: false, error: err })
} else {
res.json({success: true, result: result})
}
}
)
})

app.post('/addCollab/:docid', (req, res) => {
User.find({username: req.body.username}, (err, result) => {
if(err) {
res.json({ success: false, error: err })
} else if (result.length === 0) {
res.json({ success: false, error: "No users found" })
} else {
Document.update({_id: req.params.docid}, { $addToSet: {collaborators: req.body.username }}, (err, result) => {
if (err) {
res.send({ success: false, error: err })
} else {
res.json({success: true, result: result})
}
})
}
})
})

app.post('/searchDoc/:docid', (req, res) => {
Document.findById(req.params.docid, (err, doc) => {
if (err) {
res.send(err)
} else if (doc === null) {
res.json({success: false})
} else if (req.body.password !== doc.password) {
res.json({success: false})
} else {
Document.update({_id: req.params.docid}, { $push: {collaborators: req.user.username }}, (err, result) => {
if (err) {
res.send({ success: false, error: err })
} else {
res.json({success: true, result: doc})
}
})
}
})
})

// Document.update({_id: req.params.docid}, { $push: {collaborators: req.body.username }}, (err, result) => {
// if (err) {
// res.send({ success: false, error: err })
// } else {
// res.json({success: true, result: result})
// }
// })
// }
// })
// })

// app.post('/removeCollab/:docid', (req, res) => {
// User.find({username: req.body.username}, (err, result) => {
// if(err) {
// res.json({ success: false, error: err })
// } else if (result.length === 0) {
// res.json({ success: false, error: "No users found" })
// } else {
// Document.update({_id: req.params.docid}, { $pull: {collaborators: req.body.collaborator }}, (err, result) => {
// if (err) {
// res.send({ success: false, error: err })
// } else {
// res.json({success: true, result: result})
// }
// })
// }
// })
// })

app.listen(3000, function () {
console.log('Backend server for Electron App running on port 3000!')
})
2 changes: 2 additions & 0 deletions build/index.dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<head>
<meta charset="UTF-8">
<title>Dev Electron App</title>
<link href='https://fonts.googleapis.com/icon?family=Material+Icons'
rel='stylesheet'>
</head>
<body>
<div id="root"></div>
Expand Down
46 changes: 46 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'dev' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'predev', 'dev', 'postdev' ]
5 info lifecycle [email protected]~predev: [email protected]
6 silly lifecycle [email protected]~predev: no script for predev, continuing
7 info lifecycle [email protected]~dev: [email protected]
8 verbose lifecycle [email protected]~dev: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~dev: PATH: /usr/local/lib/node_modules/npm/bin/node-gyp-bin:/Users/staceykim/Desktop/horizons/GoogleDocs/ladydocs/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
10 verbose lifecycle [email protected]~dev: CWD: /Users/staceykim/Desktop/horizons/GoogleDocs/ladydocs
11 silly lifecycle [email protected]~dev: Args: [ '-c',
11 silly lifecycle 'concurrently -k "npm run devWebpack" "npm run devApp" "npm run devServer"' ]
12 silly lifecycle [email protected]~dev: Returned: code: 1 signal: null
13 info lifecycle [email protected]~dev: Failed to exec dev script
14 verbose stack Error: [email protected] dev: `concurrently -k "npm run devWebpack" "npm run devApp" "npm run devServer"`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/lifecycle.js:255:16)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at EventEmitter.emit (events.js:191:7)
14 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at ChildProcess.emit (events.js:191:7)
14 verbose stack at maybeClose (internal/child_process.js:920:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:230:5)
15 verbose pkgid [email protected]
16 verbose cwd /Users/staceykim/Desktop/horizons/GoogleDocs/ladydocs
17 error Darwin 14.5.0
18 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "dev"
19 error node v6.11.3
20 error npm v3.10.10
21 error code ELIFECYCLE
22 error [email protected] dev: `concurrently -k "npm run devWebpack" "npm run devApp" "npm run devServer"`
22 error Exit status 1
23 error Failed at the [email protected] dev script 'concurrently -k "npm run devWebpack" "npm run devApp" "npm run devServer"'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the electron-react-quick-start package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error concurrently -k "npm run devWebpack" "npm run devApp" "npm run devServer"
23 error You can get information on how to open an issue for this project with:
23 error npm bugs electron-react-quick-start
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls electron-react-quick-start
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]
32 changes: 26 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,42 @@
"license": "",
"devDependencies": {
"babel-core": "^6.25.0",
"babel-eslint": "^6.1.0",
"babel-loader": "^7.1.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"concurrently": "^3.5.0",
"css-loader": "^0.28.7",
"electron": "~1.6.2",
"eslint": "^3.0.1",
"eslint-plugin-react": "^5.2.2",
"loglevel": "^1.4.1",
"nodemon": "^1.11.0",
"style-loader": "^0.19.0",
"webpack": "^2.7.0",
"webpack-dev-server": "^2.6.0",
"babel-eslint": "^6.1.0",
"eslint": "^3.0.1",
"eslint-plugin-react": "^5.2.2"
"webpack-dev-server": "^2.6.0"
},
"dependencies": {
"express": "^4.15.3",
"body-parser": "^1.18.2",
"connect-mongo": "^2.0.0",
"draft-js": "^0.10.4",
"draft-js-alignment-plugin": "^2.0.0-beta1",
"draft-js-custom-styles": "^1.3.1",
"draft-js-export-html": "^1.2.0",
"draft-js-plugins-editor": "^2.0.0-beta1",
"draft-js-raw-content-state": "^1.0.7",
"express": "^4.16.2",
"express-session": "^1.15.6",
"immutable": "^3.8.2",
"material-ui": "^0.19.4",
"mongoose": "^4.12.5",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"path": "^0.12.7",
"react": "^15.6.1",
"react-dom": "^15.6.1"
"react-color": "^2.13.8",
"react-dom": "^15.6.1",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2"
}
}
11 changes: 0 additions & 11 deletions reactApp/app.js

This file was deleted.

Loading