Skip to content

Commit

Permalink
Change name and email works. Does not allow changing password if old …
Browse files Browse the repository at this point in the history
…password is incorrect
  • Loading branch information
githubalexliu committed Mar 3, 2018
1 parent 145e898 commit 1f3fc57
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 31 deletions.
44 changes: 43 additions & 1 deletion app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function(app, passport) {
if (req.isAuthenticated()){
res.render('profile', {
title: 'Profile',
message: req.flash('message'),
user: req.user
});
}
Expand All @@ -60,10 +61,27 @@ module.exports = function(app, passport) {
});

app.post('/changename', function(req, res) {
console.log(req.body.firstname);
changeName(req.user.id, req.body.firstname, req.body.lastname);
res.redirect('profile');
});

app.post('/changeemail', function(req, res) {
changeEmail(req.user.id, req.body.email);
res.redirect('profile');
});

app.post('/changepw', function(req, res, next) {
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');
}
});
// res.redirect('profile');
});

app.get('/word-list', function(req, res) {
// if (req.isAuthenticated()){
res.render('word-list', {
Expand All @@ -82,8 +100,32 @@ module.exports = function(app, passport) {
});
};

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);

function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
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) {

}
85 changes: 85 additions & 0 deletions views/modals/profile.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<div class="modal fade" id="namemodal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">

<div class="modal-header">
<h5 class="modal-title">Change Name</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form class="form-inline" action="/changename" method="post">
<div class="modal-body">
<div class="form-group">
<label for="firstname">New First Name:</label>
<input type="text" class="form-control" id="firstname" name="firstname" required>
</div>
<div class="form-group">
<label for="lastname">New Last Name:</label>
<input type="text" class="form-control" id="lastname" name="lastname" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="pwmodal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" >Change Password</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form class="form-inline" action="/changepw" method="post">
<div class="modal-body">
<div class="form-group">
<label for="oldpw">Old Password:</label>
<input type="password" class="form-control" id="oldpw" name="oldpw" required>
</div>
<div class="form-group">
<label for="lastname">New Password:</label>
<input type="password" class="form-control" id="newpw" name="newpw" required>
</div>
<div class="form-group">
<label for="newpw2">Re-enter New Password:</label>
<input type="password" class="form-control" id="newpw2" name="newpw2" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="emailmodal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Change Email</h5>
<button type="button" class="close align-right" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form class="form-inline" action="/changeemail" method="post">
<div class="modal-body">
<div class="form-group">
<label for="email">New Email:</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
38 changes: 8 additions & 30 deletions views/profile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<% if (message.length > 0) { %>
<div class="alert alert-danger top-buffer-20" role="alert">
<%= message %>
</div>
<% } %>
<table class="table table-borderless top-buffer-50">
<tbody>
<tr>
Expand Down Expand Up @@ -47,10 +52,10 @@
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#namemodal">Change Name</button>
</div>
<div class="col-4">
<button type="button" class="btn btn-primary">Change Email</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#emailmodal">Change Email</button>
</div>
<div class="col-4">
<button type="button" class="btn btn-primary">Change Password</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#pwmodal">Change Password</button>
</div>
</div>
</div>
Expand All @@ -62,34 +67,7 @@
</form>
</div>
</div>
<div class="modal fade" id="namemodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form class="form-inline" action="/changename" method="post">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Change Name</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="firstname">First Name:</label>
<input type="text" class="form-control" id="firstname" name="firstname" required>
</div>
<div class="form-group">
<label for="lastname">Last Name:</label>
<input type="text" class="form-control" id="lastname" name="lastname" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
<% include modals/profile %>
</div>
</body>

Expand Down

0 comments on commit 1f3fc57

Please sign in to comment.