diff options
author | RossTheRoss <mstrapp@protonmail.com> | 2021-05-16 21:38:59 -0500 |
---|---|---|
committer | RossTheRoss <mstrapp@protonmail.com> | 2021-05-16 21:38:59 -0500 |
commit | 9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c (patch) | |
tree | 9e739b11361f5fd122b31cfce107947502b69809 /OLD/csci4131/hw6/strap012_hw6/api/utilities.js | |
parent | Add trash (diff) | |
download | homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.tar homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.tar.gz homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.tar.bz2 homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.tar.lz homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.tar.xz homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.tar.zst homework-9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c.zip |
Rearrange files
Diffstat (limited to 'OLD/csci4131/hw6/strap012_hw6/api/utilities.js')
-rw-r--r-- | OLD/csci4131/hw6/strap012_hw6/api/utilities.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/OLD/csci4131/hw6/strap012_hw6/api/utilities.js b/OLD/csci4131/hw6/strap012_hw6/api/utilities.js new file mode 100644 index 0000000..65a087f --- /dev/null +++ b/OLD/csci4131/hw6/strap012_hw6/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;
|