-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on finishing all Schemas for mongoose
- Loading branch information
Showing
9 changed files
with
434 additions
and
74 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
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,5 @@ | ||
const cookieController = {}; | ||
|
||
cookieController.setSSIDCookie = (req, res, next) => { | ||
// set cookie called ssid to user id after user has been authenticated | ||
} |
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,11 @@ | ||
const sessionController = {}; | ||
|
||
sessionController.startSession = (req, res, next) => { | ||
// will be called after user has been authenticated. will be receiving user._id.$ | ||
// use session documents in db for now - look at other options after this is setup | ||
// create a new session document who has a cookieId set to users id. | ||
} | ||
|
||
sessionController.isLoggedIn = (req, res, next) => { | ||
// verify if user has an ssid cookie and if they have an active session in db. | ||
} |
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,6 @@ | ||
const userController = {}; | ||
|
||
|
||
userController.verifyUser = (req, res, next) => { | ||
// we will be recieving username and password in the request body. check if username exists and if so check if password matches. If match set authenticated to true in res.locals and pass along to next middleware. otherwise call global error handler with message user not authenticated and status code 401. | ||
} |
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,36 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
|
||
|
||
// sets a schema for the username/password | ||
|
||
const userSchema = new Schema({ | ||
username: { type: String, required: true, unique: true }, | ||
password: { type: String, required: true }, | ||
name: { type: String, required: true }, | ||
age: { type: Number, required: true }, | ||
userStats_id: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'health' | ||
}, | ||
}); | ||
|
||
const User = mongoose.model('user', userSchema) | ||
|
||
// sets a schema for the user health stats | ||
|
||
const statsSchema = new Schema({ | ||
height: Number, | ||
weight: { type: Number, required: true }, | ||
sex: { type: String, required: true }, | ||
targetWeight: { type: Number, required: true }, | ||
targetDate: { type: Number } | ||
}); | ||
|
||
const Stats = mongoose.mode('stats', statsSchema); | ||
|
||
/* | ||
*/ |
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,63 @@ | ||
const express = require('express'); | ||
const cookieParser = require('cookie-parser'); | ||
const mongoose = require('mongoose'); | ||
const path = require('path'); | ||
const app = express(); | ||
require('dotenv').config(); | ||
const PORT = 3000; | ||
|
||
|
||
// const mongoURI = process.env.DB_URI | ||
const mongoURI = 'mongodb+srv://jmabagat:[email protected]/' | ||
|
||
mongoose.connect(mongoURI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
dbName: 'fitness_tracker' | ||
}) | ||
.then(() => console.log('Connected to Mongo DB.')) | ||
.catch(err => console.log(err)); | ||
|
||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
app.use(cookieParser()); | ||
|
||
app.use(express.static(path.join(__dirname, '../dist'))); | ||
|
||
app.post('/login', (req, res) => { | ||
// verify user --> start session --> set ssidcookie | ||
// redirect to /dashboard, on that get rout check if user has a session (handle in sessionController.isLoggedIn middleware) | ||
}) | ||
|
||
app.post('/signup', (req, res) => { | ||
// route for user to sign up. send to userController.createUser --> startSession --> setSSIDCookie. Then redirect to /dashboard | ||
}) | ||
|
||
app.get('/dashboard', (req, res) => { | ||
// check if user is logged in | ||
}) | ||
|
||
|
||
|
||
app.get('/', (req, res) => { | ||
return res.status(200).sendFile(path.resolve(__dirname, '../dist/index.html')) | ||
}) | ||
|
||
app.use((req, res) => res.status(404).send('Error page not found')) | ||
|
||
app.use((err, req, res, next) => { | ||
const defaultErr = { | ||
log: 'Express global error handler caught unknown middleware error', | ||
status: 500, | ||
message: { err: 'An error occurred' }, | ||
}; | ||
console.log(req, res); | ||
const errObj = Object.assign({}, defaultErr, err); | ||
console.log(errObj.log); | ||
return res.status(errorObj.status).json(errObj.message); | ||
}) | ||
|
||
|
||
app.listen(PORT, () => { | ||
console.log(`Server listening on port ${PORT}`); | ||
}) |