Skip to content

Commit a80850f

Browse files
authored
Merge branch 'master' into screen_reader
2 parents 6003e31 + 8855ac5 commit a80850f

File tree

2,250 files changed

+370510
-906
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,250 files changed

+370510
-906
lines changed

.eslintignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
node_modules
1+
node_modules
2+
3+
*.js
4+
5+
*.tsx
6+
7+
*.jsx

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ yarn-error.log*
2727

2828

2929
#configuration
30-
.vscode
30+
.vscode
31+
32+
.eslintignore
33+

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"fix-path": "^3.0.0",
4747
"js-beautify": "^1.10.0",
4848
"monaco-editor": "^0.17.0",
49+
"mongoose": "^5.12.13",
4950
"node-pty": "^0.10.0",
5051
"react": "^16.8.6",
5152
"react-autosuggest": "^9.4.3",
@@ -116,5 +117,6 @@
116117
"react-test-renderer": "^16.12.0",
117118
"spectron": "^5.0.0",
118119
"test-data-bot": "^0.8.0"
119-
}
120+
},
121+
"proxy": "http://localhost:3001"
120122
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const cookieController = {};
2+
3+
cookieController.setSSIDCookie = (req, res, next) => {
4+
res.cookie('ssid', JSON.stringify(res.locals.userId).replace(/\"/g, ''));
5+
return next();
6+
};
7+
8+
cookieController.deleteCookie = (req, res, next) => {
9+
res.clearCookie('ssid');
10+
return next();
11+
};
12+
13+
module.exports = cookieController;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Session = require('../models/sessionModel');
2+
3+
const sessionController = {};
4+
5+
sessionController.startSession = (req, res, next) => {
6+
Session.create({ cookieId: res.locals.userId }, (err, result) => {
7+
if (err && err.code !== 11000) return next(err);
8+
res.locals.ssid = req.cookies.ssid;
9+
return next();
10+
});
11+
};
12+
13+
sessionController.endSession = (req, res, next) => {
14+
Session.deleteMany({ cookieId: req.cookies.ssid }, (err) => {
15+
if (err) return next(err);
16+
return next();
17+
});
18+
};
19+
20+
sessionController.isLoggedIn = (req, res, next) => {
21+
console.log('sessionController.isLoggedIn: req.cookies.ssid:', req.cookies.ssid);
22+
Session.find({ cookieId: req.cookies.ssid }, (err, data) => {
23+
if (err) return next(err);
24+
if (data.length === 0) return next('User Not Logged In');
25+
return next();
26+
});
27+
};
28+
29+
module.exports = sessionController;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const TestState = require('../models/testStateModel');
2+
3+
const testStateController = {};
4+
5+
testStateController.upload = (req, res, next) => {
6+
TestState.create(
7+
{ userId: req.cookies.ssid, testName: req.body.testName, testState: req.body.testState },
8+
(err) => {
9+
if (err) return next('Upload Failed');
10+
return next();
11+
}
12+
);
13+
};
14+
15+
testStateController.getTests = (req, res, next) => {
16+
TestState.find({ userId: req.cookies.ssid }, (err, result) => {
17+
if (err) return next(err);
18+
if (result.length === 0) return next('User Has No Saved Tests');
19+
res.locals.tests = result;
20+
return next();
21+
});
22+
};
23+
24+
module.exports = testStateController;

server/controllers/userController.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const User = require('../models/userModel');
2+
const bcrypt = require('bcrypt');
3+
4+
const userController = {};
5+
6+
// Middleware to encrypt passwords using bcrypt
7+
userController.bcrypt = (req, res, next) => {
8+
// The cost factor determines how much time is needed to calculate a single bcrypt hash
9+
const saltRounds = 10;
10+
// Destructure password from request body
11+
const { password } = req.body;
12+
// Generate the salt by passing in saltRounds (cost factor)
13+
bcrypt.genSalt(saltRounds, (err, salt) => {
14+
// Hash a password by passing in the plaintext into the hash function
15+
bcrypt.hash(password, salt, (err, hash) => {
16+
// Save encrypted password into res.locals to be accessed later
17+
res.locals.encryptedPassword = hash;
18+
return next();
19+
});
20+
});
21+
};
22+
23+
// Middleware to save user information in database
24+
userController.signup = (req, res, next) => {
25+
// collection.create method to insert new user
26+
User.create(
27+
// Pass in username from request body and encrypted password
28+
{ username: req.body.username, password: res.locals.encryptedPassword },
29+
// Callback to handle results of query
30+
(err, newUser) => {
31+
// If there is an error, invoke global error handler
32+
if (err) return next('Account already exists');
33+
// Save user ID into response locals
34+
res.locals.userId = newUser._id;
35+
// Inovke next middleware
36+
return next();
37+
}
38+
);
39+
};
40+
41+
// Middleware to check credentials and log user into application
42+
userController.login = (req, res, next) => {
43+
// Collection.find method to look for all user instances with passed username
44+
User.find({ username: req.body.username }, (err, result) => {
45+
// If there is an error, invoke global error handler
46+
if (err) return next(err);
47+
// If there are no matching usernames, invoke global error handler
48+
if (result.length === 0) return next('Incorrect username');
49+
// If there is a user with passed username, use the bcrypt.compare method to compare plaintext password with encrypted password
50+
bcrypt.compare(req.body.password, result[0].password, (err, match) => {
51+
// If an error occurs in the compare method, invoke global error handler
52+
if (err) return next(err);
53+
// If there is a match, invoke next middleware
54+
if (match) {
55+
res.locals.userId = result[0]._id;
56+
return next();
57+
}
58+
// If there is no match, invoke global error handler
59+
return next('Incorrect password');
60+
});
61+
});
62+
};
63+
64+
module.exports = userController;

server/models/sessionModel.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const mongoose = require('mongoose');
2+
const Schema = mongoose.Schema;
3+
4+
/**
5+
* Check out the `createdAt` field below. This is set up to use Mongo's automatic document
6+
* expiration service by giving the Mongoose schema the `expires` property.
7+
* After 30 seconds, the session will automatically be removed from the collection!
8+
* (actually, Mongo's cleanup service only runs once per minute so the session
9+
* could last up to 90 seconds before it's deleted, but still pretty cool!)
10+
*/
11+
const sessionSchema = new Schema({
12+
cookieId: { type: String, required: true, unique: true },
13+
createdAt: { type: Date, expires: 120, default: Date.now },
14+
});
15+
16+
module.exports = mongoose.model('Session', sessionSchema);

server/models/testStateModel.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const mongoose = require('mongoose');
2+
3+
const Schema = mongoose.Schema;
4+
5+
const testStateSchema = new Schema({
6+
userId: { type: String, require: true },
7+
testName: { type: String, require: true },
8+
testState: { type: Object },
9+
});
10+
const TestState = mongoose.model('testState', testStateSchema);
11+
12+
module.exports = TestState;

server/models/userModel.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const mongoose = require('mongoose');
2+
3+
const MONGO_URI =
4+
'mongodb+srv://ericgpark:jENk%[email protected]/myFirstDatabase?retryWrites=true&w=majority';
5+
6+
mongoose
7+
.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
8+
.then(() => console.log('Connected to Mongo DB Successfully'))
9+
.catch((err) => console.log(err));
10+
11+
const Schema = mongoose.Schema;
12+
13+
// Schema and model for 'user' collection
14+
const userSchema = new Schema({
15+
username: { type: String, require: true, unique: true },
16+
password: { type: String, require: true },
17+
});
18+
const User = mongoose.model('user', userSchema);
19+
20+
module.exports = User;

0 commit comments

Comments
 (0)