Skip to content

Commit c79fa23

Browse files
committed
create session controller, modify routes
1 parent ece5970 commit c79fa23

File tree

7 files changed

+91
-13
lines changed

7 files changed

+91
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
"copy-webpack-plugin": "^4.5.2",
177177
"cross-env": "^5.2.1",
178178
"css-loader": "^2.1.1",
179+
"dotenv": "^8.2.0",
179180
"electron": "^2.0.7",
180181
"electron-builder": "^20.44.4",
181182
"electron-devtools-installer": "^2.2.4",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const cookieController = {};
2+
3+
// setCookie = set a cookie with a random number
4+
5+
cookieController.setCookie = (req, res, next) => {
6+
// set cookie with key of 'secret' and value of a random number between 0 and 1000
7+
res.cookie('secret', Math.floor(Math.random() * 1000));
8+
return next();
9+
};
10+
11+
// setSSIDCookie - store the user id from database in cookie
12+
cookieController.setSSIDCookie = (req, res, next) => {
13+
// set cookie with key 'ssid' and value to user's id, also set http only
14+
res.cookie('ssid', res.locals.id, { httpOnly: true });
15+
return next();
16+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const Session = require('../models/sessionModel')
2+
const sessionController = {};
3+
4+
// isLoggedIn finds appropriate session for this request in database, then verifies whether or not the session is still valid
5+
sessionController.isLoggedIn = (req, res, next) => {
6+
// find cookie with current user's ssid value
7+
Session.findOne({cookieId: req.cookies.ssid}, (err, session) => {
8+
if (err) {
9+
return next({
10+
log: `Error in sessionController.isLoggedIn: ${err}`,
11+
message: {
12+
err: `Error in sessionController.isLoggedIn, check server logs for details`
13+
}
14+
})
15+
// no session found, redirect to signup page
16+
} else if (!session) {
17+
return res.redirect('/signup')
18+
} else {
19+
// session found, move onto next middleware
20+
return next();
21+
}
22+
})
23+
}
24+
25+
// startSession - create and save a new session into the database
26+
sessionController.startSession = (req, res, next) => {
27+
// if valid user logged in/signed up, res.locals.id should be user's id generated from mongodb
28+
Session.create({cookieId: res.locals.id}, (err) => {
29+
if (err) {
30+
return next({
31+
log: `Error in sessionController.startSession: ${err}`,
32+
message: {
33+
err: `Error in sessionController.startSession, check server logs for details`
34+
}
35+
})
36+
}
37+
return next();
38+
})
39+
}
40+
41+
module.exports = sessionController;

server/controllers/userController.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// middleware functions create a new user and verify users
22

3-
const Users = require('../models/userModels');
3+
const Users = require('../models/userModel');
44

55
const userController = {};
66
const bcrypt = require('bcryptjs');
77

88
userController.createUser = (req, res, next) => {
9+
console.log('Inside createUser')
910
const { username, password } = req.body;
1011
// error handling if username or password is missing
1112
if (!username || !password) {

server/models/sessionModel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const mongoose = require('mongoose');
22
const Schema = mongoose.Schema;
33

4+
// Mongo has an automatic document expiration service that we can use via the 'expires' property in the schema. This sets it so each session can only last an hour
45
const sessionSchema = new Schema({
56
cookieId: { type: String, required: true, unique: true },
67
createdAt: { type: Date, expires: 3600, default: Date.now }

server/models/userModels.js renamed to server/models/userModel.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
const mongoose = require('mongoose');
2-
3-
mongoose
4-
.connect(process.env.MONGO_URI, {
5-
// options for the connect method to parse the URI
6-
useNewUrlParser: true,
7-
useUnifiedTopology: true,
8-
// sets the name of the DB that our collections are part of
9-
dbName: 'ReacType'
10-
})
11-
.then(() => console.log('Connected to Mongo DB.'))
12-
.catch(err => console.log(err));
2+
require('dotenv').config();
133

144
const Schema = mongoose.Schema;
155

server/server.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
const express = require('express');
2+
const mongoose = require('mongoose')
23
const path = require('path');
34
const cookieParser = require('cookie-parser');
5+
const userController = require('./controllers/userController')
6+
const cookieController = require('./controllers/cookieController')
47
const app = express();
58
const PORT = 8080;
69

10+
// connect to mongo db
11+
mongoose
12+
.connect(process.env.MONGO_URI, {
13+
// options for the connect method to parse the URI
14+
useNewUrlParser: true,
15+
useUnifiedTopology: true,
16+
useCreateIndex: true,
17+
// sets the name of the DB that our collections are part of
18+
dbName: 'ReacType'
19+
})
20+
.then(() => console.log('Connected to Mongo DB.'))
21+
.catch(err => console.log(err));
22+
723
// handle parsing request body
824
app.use(express.json());
925
// cookie parser
1026
app.use(cookieParser());
1127

1228
// statically serve everything in build folder
13-
app.use('/', express.statice(path.resolve(__dirname, '../build')));
29+
app.use('/build', express.static(path.resolve(__dirname, '../build')));
30+
31+
// app.get('/', cookieController.setCookie, (req, res) => {
32+
// res.status(200).sendFile('../build/index.html');
33+
// })
34+
35+
app.post('/signup', userController.createUser, (req, res) => {
36+
return res.status(200).json(res.locals.newUser)
37+
})
38+
39+
app.post('/login', userController.verifyUser, (req, res) => {
40+
return res.status(200).json(res.locals.id)
41+
})
1442

1543
// catch-all route handler
1644
app.use('*', (req, res) => {

0 commit comments

Comments
 (0)