aboutsummaryrefslogtreecommitdiffstats
path: root/csci4131/hw6/strap012_hw6
diff options
context:
space:
mode:
Diffstat (limited to 'csci4131/hw6/strap012_hw6')
-rw-r--r--csci4131/hw6/strap012_hw6/api/utilities.js33
-rw-r--r--csci4131/hw6/strap012_hw6/dbio.js71
-rw-r--r--csci4131/hw6/strap012_hw6/index.js6
-rw-r--r--csci4131/hw6/strap012_hw6/login.html38
4 files changed, 142 insertions, 6 deletions
diff --git a/csci4131/hw6/strap012_hw6/api/utilities.js b/csci4131/hw6/strap012_hw6/api/utilities.js
index eda6e72..8a97d20 100644
--- a/csci4131/hw6/strap012_hw6/api/utilities.js
+++ b/csci4131/hw6/strap012_hw6/api/utilities.js
@@ -1,11 +1,42 @@
const express = require('express')
+const db = require ('../dbio')
const router = express.Router()
router.get('/contacts', function (req, res) {
// TODO: Implement code to fetch contacts from the database
- res.send("AAAAAAAAAAAA")
+ table = db.getContacts();
+ res.send("AAAAAAAAAAAA");
});
+
// TODO: Add implementation for other necessary end-points
+router.post('/login', function(req, res) {
+ var loginInfo = req.body;
+ var login = loginInfo.login;
+ var pwd = loginInfo.password;
+
+ // Query the database tbl_login with login and hashed password
+ rows = db.query(login,pwd);
+ // 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'});
+ });
+
+router.get('/logout', function(req, res) {
+ if(!req.session.value) {
+ res.send('Session not started, can not logout!');
+ } else {
+ console.log ("Successfully Destroyed Session!");
+ req.session.destroy();
+ res.send("Session Complete!");
+ res.redirect('/login');
+ }
+});
module.exports = router;
diff --git a/csci4131/hw6/strap012_hw6/dbio.js b/csci4131/hw6/strap012_hw6/dbio.js
new file mode 100644
index 0000000..538fa5a
--- /dev/null
+++ b/csci4131/hw6/strap012_hw6/dbio.js
@@ -0,0 +1,71 @@
+var mysql = require("mysql");
+var bcrypt = require("bcrypt");
+
+var connection = mysql.createConnection({
+ host: "cse-mysql-classes-01.cse.umn.edu",
+ user: "C4131S21U83",
+ password: "6919",
+ database: "C4131S21U83",
+ port: 3306
+});
+
+connection.connect(function(err) {
+ if (err) {
+ throw err;
+ };
+ console.log("Connected to MYSQL database!");
+});
+
+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++) {
+ if (rows[i].acc_login.localeCompare(user) === 0) {
+ if (bcrypt.compareSync(pass, rows[i].acc_password)) {
+ ret += rows[i];
+ }
+ }
+ }
+ }
+ });
+ return ret;
+}
+
+function getContacts() {
+ let conTab = []
+ connection.query('SELECT * FROM tbl_contacts', function(err,rows,fields) {
+ for (let i=0; i<rows.length; i++) {
+ conTab[i] = rows[i];
+ }
+ });
+ return conTab;
+}
+
+// Parameterized Insert
+var rowToBeInserted = {
+ Title: 'A Book', // Dummy Book Name
+ Category: 'General', // Dummy Category Type
+ ISBN : '0000001234'// Dummy
+ };
+
+//connection.query('INSERT books SET ?', rowToBeInserted, function(err, result) { //Parameterized insert
+// if(err) throw err;
+// console.log("Values inserted");
+// });
+
+var Title = 'Another Book';
+var Cat = 'Fiction';
+var ISBN = '0000002345';
+
+//var sql = 'INSERT INTO books (Title,Category,ISBN) VALUES (' + '"' + Title + '"' + ',' + '"' + Cat + '"' + ',' + '"' + ISBN + '"' + ')';
+//connection.query(sql,function(err,result) {
+// if (err) throw err;
+// console.log ("Version 2 values inserted");
+// });
+
+
diff --git a/csci4131/hw6/strap012_hw6/index.js b/csci4131/hw6/strap012_hw6/index.js
index 965b23e..474b165 100644
--- a/csci4131/hw6/strap012_hw6/index.js
+++ b/csci4131/hw6/strap012_hw6/index.js
@@ -76,14 +76,10 @@ app.get('/login', function (req, res) {
if (req.session.value) {
res.redirect('/contacts');
} else {
- res.sendFile(path.join(__dirname, 'public/login.html'));
+ res.sendFile(path.join(__dirname, 'login.html'));
}
});
-app.post('/logintry', function(req, res) {
-
-});
-
// Makes Express use a router called utilities
app.use('/api', utilities);
diff --git a/csci4131/hw6/strap012_hw6/login.html b/csci4131/hw6/strap012_hw6/login.html
new file mode 100644
index 0000000..59e7dfa
--- /dev/null
+++ b/csci4131/hw6/strap012_hw6/login.html
@@ -0,0 +1,38 @@
+<html>
+
+<head>
+ <script src="https://code.jquery.com/jquery-2.2.4.min.js"integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
+</head>
+
+<body>
+ <form id="myForm" name="myForm">
+ <div>
+ <label for="login">login:</label>
+ <input type="text" id="login" name="login" required>
+ </div>
+ <div>
+ <label for="password">password:</label>
+ <input id="password" name="password" type="password" required>
+ </div>
+ <input type="submit"value="Submit!">
+ </form>
+ <script>
+ $(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) => {
+ console.log(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
+ }});});});</script>
+
+</html>