From f68c5b0c2407431d1b23c0f35688e0a794ef4e13 Mon Sep 17 00:00:00 2001 From: Lucas Patenaude Date: Wed, 10 Apr 2024 23:09:30 -0600 Subject: [PATCH] Fix to index.js to fix code differences after merge --- ProjectSourceCode/src/index.js | 43 +++++++++++------------ ProjectSourceCode/src/resources/test.html | 11 ------ 2 files changed, 21 insertions(+), 33 deletions(-) delete mode 100644 ProjectSourceCode/src/resources/test.html diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index 052e073..1990fc2 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -158,29 +158,28 @@ app.get('/', (req, res) => { res.render('pages/register'); }); - // Trigger Registration Form to Post - app.post('/register', async (req, res) => { - try { - if (!req.body.username || !req.body.password) { - // If username or password is missing, respond with status 400 and an error message - return res.status(400).json({status: 'error', message: 'Invalid input'}); - } - // Hash the password using bcrypt library - const hash = await bcrypt.hash(req.body.password, 10); - - // Insert username and hashed password into the 'users' table - await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); - //res.status(200); - res.json({status: 'success', message: 'Success'}); - // Direct user to login screen after data has been inserted successfully - res.redirect('/login'); - } catch (error) { - // If the insert fails, redirect to GET /register route - res.status(400); - //res.json({status: 'success', message: 'Invalid input'}); - res.redirect('/register'); + +// Trigger Registration Form to Post +app.post('/register', async (req, res) => { + try { + if (!req.body.username || !req.body.password) { + // If username or password is missing, respond with status 400 and an error message + return res.status(400).json({ status: 'error', message: 'Invalid input' }); } - }); + // Hash the password using bcrypt library + const hash = await bcrypt.hash(req.body.password, 10); + + // Insert username and hashed password into the 'users' table + await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); + + // Redirect user to the login page after successful registration + res.redirect('/login'); + } catch (error) { + // If an error occurs during registration, respond with status 500 and an error message + res.status(500).json({ status: 'error', message: 'An error occurred during registration' }); + } +}); + /************************ Home Page Routes diff --git a/ProjectSourceCode/src/resources/test.html b/ProjectSourceCode/src/resources/test.html deleted file mode 100644 index 55a2ff3..0000000 --- a/ProjectSourceCode/src/resources/test.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - Document - - - - \ No newline at end of file