Skip to content

Commit

Permalink
Merge pull request #4 from WCRI56-Team-GS/sammichael/login
Browse files Browse the repository at this point in the history
Add basic controller and model functionality
  • Loading branch information
gacetta authored Mar 4, 2023
2 parents df5c2f6 + 1fae969 commit 10a9f52
Show file tree
Hide file tree
Showing 10 changed files with 536 additions and 7 deletions.
363 changes: 357 additions & 6 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^7.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.8.2",
Expand Down
Empty file removed server/controllers/controller.js
Empty file.
22 changes: 22 additions & 0 deletions server/controllers/cookieController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const cookieController = {};
/**
* setCookie - set a cookie with a random number
*/
cookieController.setCookie = (req, res, next) => {
console.log('running cookieController.setCookie');
res.cookie('secret', Math.floor(Math.random() * 100).toString(), { secure: true, httpOnly: true});

return next();
}

/**
* setSSIDCookie - store the user id in a cookie
*/
cookieController.setSSIDCookie = (req, res, next) => {
console.log('running cookieController.setSSIDCookie');

res.cookie('ssid', res.locals.user._id.toString(), { secure: true, httpOnly: true });
return next();
}

module.exports = cookieController;
26 changes: 26 additions & 0 deletions server/controllers/sessionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Session = require('../models/sessionModel');

const sessionController = {};

/**
* isLoggedIn - find the appropriate session for this request in the database, then
* verify whether or not the session is still valid.
*/
sessionController.isLoggedIn = (req, res, next) => {
// write code here

};

/**
* startSession - create and save a new Session into the database.
*/
sessionController.startSession = (req, res, next) => {
console.log('sessionController.startSession');
Session.create({ cookieId: res.locals.user._id.toString() }, (err, data) => {
if (err) return next('Error in sessionController.startSession: ' + JSON.stringify(err));
console.log('created new session: ', data)
return next();
})
};

module.exports = sessionController;
38 changes: 38 additions & 0 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const User = require('../models/userModel');
const path = require('path');

const userController = {};

/**
* verifyUser - Obtain username and password from the request body, locate
* the appropriate user in the database, and then authenticate the submitted password
* against the password stored in the database.
*/
userController.verifyUser = (req, res, next) => {
console.log('running userController.verifyUser');

const { username, password } = req.body

// ERROR HANDLING
if (!username || !password) {
console.log('Error in userController.verifyUser: username and password must be provided');
return next('username and password must be provided');
}

// check if req.body.username matches a username in the database
User.findOne({ username }, (err, user) => {
// database error
if (err) return next('Error in userController.verifyUser: ' + JSON.stringify(err));

// no user was found OR passwords do not match
else if (!user || password !== user.password) return res.redirect('/signup');

// valid user
else {
res.locals.user = user;
return next();
}
});
};

module.exports = userController;
25 changes: 25 additions & 0 deletions server/models/boardModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

/**
* Check out the `createdAt` field below. This is set up to use Mongo's automatic document
* expiration service by giving the Mongoose schema the `expires` property.
* After 30 seconds, the session will automatically be removed from the collection!
* (actually, Mongo's cleanup service only runs once per minute so the session
* could last up to 90 seconds before it's deleted, but still pretty cool!)
*/
const boardSchema = new Schema({
boardName: { type: String, required: true, unique: true },
columns: [
{
columnName: { type: String, required: true, unique: true },
cards: [
{
cardText: { type: String, required: true, unique: true }
}
]
}
]
});

module.exports = mongoose.model('Session', sessionSchema);
16 changes: 16 additions & 0 deletions server/models/sessionModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

/**
* Check out the `createdAt` field below. This is set up to use Mongo's automatic document
* expiration service by giving the Mongoose schema the `expires` property.
* After 30 seconds, the session will automatically be removed from the collection!
* (actually, Mongo's cleanup service only runs once per minute so the session
* could last up to 90 seconds before it's deleted, but still pretty cool!)
*/
const sessionSchema = new Schema({
cookieId: { type: String, required: true, unique: true },
createdAt: { type: Date, expires: 30, default: Date.now }
});

module.exports = mongoose.model('Session', sessionSchema);
26 changes: 26 additions & 0 deletions server/models/userModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// bcrypt
// const SALT_WORK_FACTOR = 10;
// const bcrypt = require('bcryptjs');

const userSchema = new Schema({
username: {type: String, required: true, unique: true},
password: {type: String, required: true},
board_ids: [Number]
});

// userSchema.pre('save', function(next) {
// // bcrypt.hash()
// console.log('PRE SAVE', this.password);
// return next();
// });
// userSchema.pre('find', function(next) {
// // bcrypt.hash()
// //'this' is not pulling the find inputs - why?
// console.log('PRE FIND', this);
// return next();
// })

module.exports = mongoose.model('User', userSchema);
26 changes: 25 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
const path = require('path');
const express = require("express");
const cors = require("cors");
const mongoose = require('mongoose')
const userController = require('./controllers/userController')

// setup app and port
const app = express();
const PORT = process.env.PORT || 3000;

const mongoURI = 'mongodb+srv://shendo87:UIOqlCfrXxZJYeJL@cluster0.kzkmgom.mongodb.net/?retryWrites=true&w=majority';
mongoose.connect(mongoURI, {
// options for the connect method to parse the URI
useNewUrlParser: true,
useUnifiedTopology: true,
// sets the name of the DB that our collections are part of
dbName: 'scratch_project'
})
.then(() => console.log('Connected to Mongo DB.'))
.catch(err => console.log(err));

// handle parsing request body
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
Expand All @@ -17,7 +30,18 @@ app.use(cors());
app.use("/build", express.static(path.resolve(__dirname, "../build")));

// define route handlers

/**
* login
*/
app.post('/login',
userController.verifyUser,
// sessionController.startSession,
// cookieController.setSSIDCookie,
(req, res) => {
// what should happen here on successful log in?
res.redirect('/secret');
console.log('request to login')
});

// server index.html
app.get("/", (req, res) => {
Expand Down

0 comments on commit 10a9f52

Please sign in to comment.