From 0c7bb17b8c955254c2db61a5fe9c8240ea1357e2 Mon Sep 17 00:00:00 2001 From: Matthew Strapp Date: Fri, 16 Apr 2021 21:37:26 -0500 Subject: finish hw6 --- csci4131/hw6/strap012_hw6/api/utilities.js | 34 +++++----- csci4131/hw6/strap012_hw6/dbio.js | 79 ++++++++++++------------ csci4131/hw6/strap012_hw6/index.js | 16 +++-- csci4131/hw6/strap012_hw6/login.html | 46 ++++++++------ csci4131/hw6/strap012_hw6/public/addContact.html | 21 +++---- csci4131/hw6/strap012_hw6/public/contacts.html | 25 +++++--- csci4131/hw6/strap012_hw6/public/stock.html | 9 +-- csci4131/hw6/strap012_hw6/public/welcome.html | 1 - 8 files changed, 125 insertions(+), 106 deletions(-) (limited to 'csci4131/hw6') diff --git a/csci4131/hw6/strap012_hw6/api/utilities.js b/csci4131/hw6/strap012_hw6/api/utilities.js index 8036c29..65a087f 100644 --- a/csci4131/hw6/strap012_hw6/api/utilities.js +++ b/csci4131/hw6/strap012_hw6/api/utilities.js @@ -4,13 +4,11 @@ const router = express.Router() router.use(express.urlencoded({ extended: true })) router.get('/contacts', function (req, res) { - // TODO: Implement code to fetch contacts from the database - table = db.getContacts(); - res.send("AAAAAAAAAAAA"); + db.getContacts().then(function(table) { + res.send(table) + }); }); - -// TODO: Add implementation for other necessary end-points router.post('/login', async function(req, res) { var loginInfo = req.body; var login = loginInfo.login; @@ -18,31 +16,33 @@ router.post('/login', async function(req, res) { let rows = []; // Query the database tbl_login with login and hashed password - try { - rows = await db.query(login, pwd); - } - finally { + db.query(login, pwd).then(function(rows) { // Provided there is no error, and the results set is assigned to a variable named rows: if (rows.length >= 1) {// the length should be 0 or 1, but this will work for now //success, set the session, return success req.session.user = login; res.json({ status: 'success' }); - res.send("SUCC"); - } else - res.send("FAIL"); - res.json({ status: 'fail' }); - } + } else { + res.json({ status: 'fail' }); + } + }); + }); router.get('/logout', function(req, res) { - if(!req.session.value) { + if(!req.session.user) { res.send('Session not started, can not logout!'); } else { - console.log ("Successfully Destroyed Session!"); req.session.destroy(); - res.send("Session Complete!"); res.redirect('/login'); } }); +router.post('/addContact', function(req, res) { + var contact = req.body; + db.addContact(contact).then(function() { + res.redirect('/contacts'); + }); +}); + module.exports = router; diff --git a/csci4131/hw6/strap012_hw6/dbio.js b/csci4131/hw6/strap012_hw6/dbio.js index ab3ec42..302334b 100644 --- a/csci4131/hw6/strap012_hw6/dbio.js +++ b/csci4131/hw6/strap012_hw6/dbio.js @@ -16,56 +16,59 @@ connection.connect(function(err) { console.log("Connected to MYSQL database!"); }); -async function passcheck(user,pass) { - let ret = ''; - connection.query('SELECT * FROM tbl_accounts', function(err,rows,fields) { - if (err) throw err; - if (rows.length == 0) { - console.log("There are no entries in the accounts field!"); - } else { - for (var i = 0 ; i < rows.length; i++) { +function passcheck(user,pass) { + return new Promise(function(resolve, reject) { + connection.query('SELECT * FROM tbl_accounts', function(err, rows, fields) { + let ret = []; + if (err) { + return reject(err); + } + for (var i = 0; i < rows.length; i++) { if (rows[i].acc_login.localeCompare(user) === 0) { if (bcrypt.compareSync(pass, rows[i].acc_password)) { - ret += rows[i]; + ret += rows[i]; } } } - } - return ret; + resolve(ret); + }); }); - } function getContacts() { - let conTab = [] - connection.query('SELECT * FROM tbl_contacts', function(err,rows,fields) { - for (let i=0; i + + -
+
+

Login Page

+
+

+
@@ -16,23 +22,27 @@
+
+ //jQuery below. Tread with caution as sneezing near here may rupture the fabric of reality. + $(document).ready(function () { + $('#myForm').submit(function (event) { + event.preventDefault();//collect the form data using Id Selector for whatever data you need to send to server + let login=$('#login').val(); + let password=$('#password').val(); + $.post('api/login', + {"login": login,"password": password}, + (data) => { + if(data.status === 'success'){ + //pseudo code + //Make sure error message is not displayed + //Re-direct to contacts page, + window.location.href='contacts';} + else{ + //Display error message + $('#error').html("Login failed. Please try again.") + }}); + }); + }); diff --git a/csci4131/hw6/strap012_hw6/public/addContact.html b/csci4131/hw6/strap012_hw6/public/addContact.html index 12bfaa2..629f9b2 100644 --- a/csci4131/hw6/strap012_hw6/public/addContact.html +++ b/csci4131/hw6/strap012_hw6/public/addContact.html @@ -13,10 +13,11 @@ @@ -31,7 +32,7 @@
-
+

@@ -83,15 +84,7 @@ - - - - diff --git a/csci4131/hw6/strap012_hw6/public/contacts.html b/csci4131/hw6/strap012_hw6/public/contacts.html index 15e4032..5cd2907 100644 --- a/csci4131/hw6/strap012_hw6/public/contacts.html +++ b/csci4131/hw6/strap012_hw6/public/contacts.html @@ -8,7 +8,7 @@ - Welcome to Node.js
Website Name
- -
-
Website URL -
- +