From a8658881582f1be34a772af3ab05b0d82eb86e88 Mon Sep 17 00:00:00 2001 From: alexfromgithub Date: Sun, 4 Mar 2018 09:46:33 -0500 Subject: [PATCH] Profile page done. Attempt to make a profile module --- app/profile.js | 18 ++++++++++++++ app/routes.js | 56 ++++++++++++++++++------------------------ public/css/styles.css | 17 +++++++++++++ views/partials/nav.ejs | 52 ++++++++++++++++++++++----------------- views/profile.ejs | 9 +++++-- 5 files changed, 95 insertions(+), 57 deletions(-) create mode 100644 app/profile.js diff --git a/app/profile.js b/app/profile.js new file mode 100644 index 0000000..2b1377c --- /dev/null +++ b/app/profile.js @@ -0,0 +1,18 @@ +module.exports = function(req, res) { + changeName = function(id, firstname, lastname) { + connection.query("UPDATE users SET firstname = ?, lastname = ? WHERE id = ?", [firstname, lastname, id], function(err, rows) { + if (err) throw err; + }); + } + + changeEmail = function(id, email) { + connection.query("UPDATE users SET email = ? WHERE id = ?", [email, id], function(err, rows) { + if (err) throw err; + }); + } +}; +var mysql = require('mysql'); +var dbconfig = require('../config/database'); +var bcrypt = require('bcrypt-nodejs'); +var connection = mysql.createConnection(dbconfig.connection); +connection.query('USE ' + dbconfig.database); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js index 4018ad7..8205abf 100644 --- a/app/routes.js +++ b/app/routes.js @@ -48,14 +48,14 @@ module.exports = function(app, passport) { }); app.get('/profile', function(req, res) { - if (req.isAuthenticated()){ + if (req.isAuthenticated()) { res.render('profile', { title: 'Profile', - message: req.flash('message'), + messageSuccess: req.flash('messageSuccess'), + messageFail: req.flash('messageFail'), user: req.user }); - } - else { + } else { res.redirect('/'); } }); @@ -70,24 +70,33 @@ module.exports = function(app, passport) { res.redirect('profile'); }); - app.post('/changepw', function(req, res, next) { + app.post('/changepw', function(req, res) { + // Is there a way to move this into profile.js? + // If I move the redirect out of connection.query, the messages are not flashed + // I can't move redirect into the module connection.query("SELECT * FROM users WHERE id = ?", [req.user.id], function(err, rows) { if (err) throw err; - if (req.body.oldpw !== rows[0].password) { - console.log("dd"); - req.flash('message', 'Sorry. Your old password is incorrect.'); - res.redirect('profile'); + else if (!bcrypt.compareSync(req.body.oldpw, rows[0].password)) { + req.flash('messageFail', 'Your old password is incorrect.'); + } else if (req.body.newpw !== req.body.newpw2) { + req.flash('messageFail', 'Your new passwords do not match.'); + } else { + connection.query("UPDATE users SET password = ? WHERE id = ?", [bcrypt.hashSync(req.body.newpw, null, null), req.user.id], function(err, rows) { + if (err) throw err; + }); + req.flash('messageSuccess', 'Your password has been changed. Please \ + logout for your new password to take effect.'); } + res.redirect('profile'); }); - // res.redirect('profile'); }); app.get('/word-list', function(req, res) { // if (req.isAuthenticated()){ - res.render('word-list', { - title: 'Word List', - user: req.user - }); + res.render('word-list', { + title: 'Word List', + user: req.user + }); // } // else { // res.redirect('/'); @@ -104,6 +113,7 @@ var mysql = require('mysql'); var dbconfig = require('../config/database'); var bcrypt = require('bcrypt-nodejs'); var connection = mysql.createConnection(dbconfig.connection); +require('./profile.js')(); connection.query('USE ' + dbconfig.database); function isLoggedIn(req, res, next) { @@ -111,21 +121,3 @@ function isLoggedIn(req, res, next) { return next(); res.redirect('/'); } - -function changeName(id, firstname, lastname) { - connection.query("UPDATE users SET firstname = ?, lastname = ? WHERE id = ?", - [firstname,lastname,id], function(err, rows){ - if (err) throw err; - }); -} - -function changeEmail(id, email) { - connection.query("UPDATE users SET email = ? WHERE id = ?", - [email,id], function(err, rows){ - if (err) throw err; - }); -} - -function changePW(id, oldpw, newpw, newpw2, done, req) { - -} diff --git a/public/css/styles.css b/public/css/styles.css index 807ef52..b30d037 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -16,3 +16,20 @@ .form-inline * { margin-bottom: 10px; } +.navbar-custom { + background-color: #5B9BD5; +} +/* change the brand and text color */ +.navbar-custom .navbar-brand, +.navbar-custom .navbar-text { + color: rgba(255,255,255,.8); +} +/* change the link color */ +.navbar-custom .navbar-nav .nav-link { + color: rgba(255,255,255,.5); +} +/* change the color of active or hovered links */ +.navbar-custom .nav-item.active .nav-link, +.navbar-custom .nav-item:hover .nav-link { + color: #ffffff; +} diff --git a/views/partials/nav.ejs b/views/partials/nav.ejs index 2115015..210b976 100644 --- a/views/partials/nav.ejs +++ b/views/partials/nav.ejs @@ -1,34 +1,40 @@ - \ No newline at end of file diff --git a/views/profile.ejs b/views/profile.ejs index d40cc45..84d3277 100644 --- a/views/profile.ejs +++ b/views/profile.ejs @@ -10,11 +10,16 @@
- <% if (message.length > 0) { %> + <% if (messageFail.length > 0) { %> <% } %> + <% if (messageSuccess.length > 0) { %> + + <% } %>