diff --git a/app.js b/app.js index 299c15a..8497609 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,4 @@ +// Import required libraries const express = require('express'); const expressLayouts = require('express-ejs-layouts'); const mongoose = require('mongoose'); @@ -63,4 +64,5 @@ app.use('/users', require('./routes/users.js')); const PORT = process.env.PORT || 5000; +// Start Server app.listen(PORT, console.log(`Server started on port ${PORT}`)); diff --git a/routes/users.js b/routes/users.js index d5c4205..6e85112 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,11 +1,14 @@ +// Imports libraries const express = require('express'); const router = express.Router(); const bcrypt = require('bcryptjs'); const passport = require('passport'); +const ObjectID = require('mongodb').ObjectID; // Load User model const User = require('../models/User'); const { forwardAuthenticated } = require('../config/auth'); +// Loads libraries for uploading img functions const fs = require('fs'); const path = require('path'); const multer = require('multer'); @@ -30,24 +33,80 @@ router.get('/register', forwardAuthenticated, (req, res) => res.render('register // Edit page router.get('/edit', (req, res) => res.render('edit')); +// Edit +router.post('/edit', upload.single('image'), (req, res, next) => { + // Gets inputs + const { _id, name, currPassword, newPassword, newPassword2 } = req.body; + let errors = []; + + // Get specified user from db + let currUser = User.findOne({ _id }); + + // Checks if blank + // if (!name || !email || !currPassword || !newPassword || !newPassword2) { + // errors.push({ msg: 'Please enter all fields' }); + // } + + // Checks if new password & new password confirm match + if (newPassword != newPassword2) { + errors.push({ msg: 'New Passwords do not match' }); + } + + // Validates new password is required lenght + if (newPassword.length < 6) { + errors.push({ msg: 'Password must be at least 6 characters' }); + } + + // Error Handling + if (errors.length > 0) { + // Redirects to edit page with errors + res.render('edit', { + errors, + name, + currPassword, + newPassword, + newPassword2 + }); + } else { + //creates updated user object + let obj = { + name: name, + image: { + data: fs.readFileSync(path.join(__dirname + '/uploads/' + req.file.filename)), + contentType: 'image/png' + }, + password: newPassword + } + //Updates user and redirects to dashboard + User.updateOne({ _id }, { $set: obj}); + res.redirect('/dashboard'); + } +}); + // Register router.post('/register', upload.single('image'), (req, res, next) => { + // Collects input const { name, email, password, password2 } = req.body; let errors = []; + // Check for blanks if (!name || !email || !password || !password2) { errors.push({ msg: 'Please enter all fields' }); } + // Check if password is confirmed if (password != password2) { errors.push({ msg: 'Passwords do not match' }); } + // Validates password is to required length if (password.length < 6) { errors.push({ msg: 'Password must be at least 6 characters' }); } + // Error handling if (errors.length > 0) { + // Redirects to register page with errors res.render('register', { errors, name, @@ -56,9 +115,11 @@ router.post('/register', upload.single('image'), (req, res, next) => { password2 }); } else { + // Verifies Email isnt taken User.findOne({ email: email }).then(user => { if (user) { errors.push({ msg: 'Email already exists' }); + // Redirect to register page with errors res.render('register', { errors, name, @@ -67,6 +128,7 @@ router.post('/register', upload.single('image'), (req, res, next) => { password2 }); } else { + // Creates new User Object const newUser = new User({ name, image: { @@ -77,6 +139,7 @@ router.post('/register', upload.single('image'), (req, res, next) => { password, }); + // Encrypts password bcrypt.genSalt(10, (err, salt) => { bcrypt.hash(newUser.password, salt, (err, hash) => { if (err) throw err; @@ -88,6 +151,7 @@ router.post('/register', upload.single('image'), (req, res, next) => { 'success_msg', 'You are now registered and can log in' ); + // Takes to login page on success res.redirect('/users/login'); }) .catch(err => console.log(err)); diff --git a/views/dashboard.ejs b/views/dashboard.ejs index bc6c404..af32f5e 100644 --- a/views/dashboard.ejs +++ b/views/dashboard.ejs @@ -1,3 +1,4 @@ + <% if (user.role == "Teacher") { %> <% include teacherDashboard %> <% } %> diff --git a/views/edit.ejs b/views/edit.ejs new file mode 100644 index 0000000..f1f9126 --- /dev/null +++ b/views/edit.ejs @@ -0,0 +1,78 @@ +
+
+
+

+ Edit Account +

+ <% include ./partials/messages %> +
+ + + + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+
diff --git a/views/studentDashboard.ejs b/views/studentDashboard.ejs index fb78113..15f8fc4 100644 --- a/views/studentDashboard.ejs +++ b/views/studentDashboard.ejs @@ -1,3 +1,4 @@ + + +

Student Dashboard

Welcome <%= user.name %>

+
+
+
+ '> +
+

+

+ +

XP: <%= user.xp %>


-
-
-
-

-
-
- '> -
-

+ + Logout + + + + + + + +

Site Members

+ +