aboutsummaryrefslogtreecommitdiffstats
path: root/csci4131/hw7/api
diff options
context:
space:
mode:
authorRossTheRoss <mstrapp@protonmail.com>2021-04-30 08:41:38 -0500
committerRossTheRoss <mstrapp@protonmail.com>2021-04-30 08:41:38 -0500
commit05c691be64d1356b0a996d1c2a2f5b0286653c85 (patch)
treedadbde32d1384913fc981982b2c74eef098eeef5 /csci4131/hw7/api
parentAdd HW5 to git (diff)
parentAdd HW6 files (diff)
downloadhomework-05c691be64d1356b0a996d1c2a2f5b0286653c85.tar
homework-05c691be64d1356b0a996d1c2a2f5b0286653c85.tar.gz
homework-05c691be64d1356b0a996d1c2a2f5b0286653c85.tar.bz2
homework-05c691be64d1356b0a996d1c2a2f5b0286653c85.tar.lz
homework-05c691be64d1356b0a996d1c2a2f5b0286653c85.tar.xz
homework-05c691be64d1356b0a996d1c2a2f5b0286653c85.tar.zst
homework-05c691be64d1356b0a996d1c2a2f5b0286653c85.zip
Merge branch 'master' of github.com:RosstheRoss/TestingFun
Diffstat (limited to '')
-rw-r--r--csci4131/hw7/api/utilities.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/csci4131/hw7/api/utilities.js b/csci4131/hw7/api/utilities.js
new file mode 100644
index 0000000..65a087f
--- /dev/null
+++ b/csci4131/hw7/api/utilities.js
@@ -0,0 +1,48 @@
+const express = require('express')
+const db = require ('../dbio')
+const router = express.Router()
+router.use(express.urlencoded({ extended: true }))
+
+router.get('/contacts', function (req, res) {
+ db.getContacts().then(function(table) {
+ res.send(table)
+ });
+});
+
+router.post('/login', async function(req, res) {
+ var loginInfo = req.body;
+ var login = loginInfo.login;
+ var pwd = loginInfo.password;
+ let rows = [];
+
+ // Query the database tbl_login with login and hashed password
+ 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' });
+ } else {
+ res.json({ status: 'fail' });
+ }
+ });
+
+});
+
+router.get('/logout', function(req, res) {
+ if(!req.session.user) {
+ res.send('Session not started, can not logout!');
+ } else {
+ req.session.destroy();
+ res.redirect('/login');
+ }
+});
+
+router.post('/addContact', function(req, res) {
+ var contact = req.body;
+ db.addContact(contact).then(function() {
+ res.redirect('/contacts');
+ });
+});
+
+module.exports = router;