-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from WCRI56-Team-GS/sammichael/login
Add basic controller and model functionality
- Loading branch information
Showing
10 changed files
with
536 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters