Fix to index.js to fix code differences after merge

This commit is contained in:
Lucas Patenaude
2024-04-10 23:09:30 -06:00
parent e8760282a6
commit f68c5b0c24
2 changed files with 21 additions and 33 deletions

View File

@@ -158,29 +158,28 @@ app.get('/', (req, res) => {
res.render('pages/register'); res.render('pages/register');
}); });
// Trigger Registration Form to Post
app.post('/register', async (req, res) => { // Trigger Registration Form to Post
app.post('/register', async (req, res) => {
try { try {
if (!req.body.username || !req.body.password) { if (!req.body.username || !req.body.password) {
// If username or password is missing, respond with status 400 and an error message // If username or password is missing, respond with status 400 and an error message
return res.status(400).json({status: 'error', message: 'Invalid input'}); return res.status(400).json({ status: 'error', message: 'Invalid input' });
} }
// Hash the password using bcrypt library // Hash the password using bcrypt library
const hash = await bcrypt.hash(req.body.password, 10); const hash = await bcrypt.hash(req.body.password, 10);
// Insert username and hashed password into the 'users' table // Insert username and hashed password into the 'users' table
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
//res.status(200);
res.json({status: 'success', message: 'Success'}); // Redirect user to the login page after successful registration
// Direct user to login screen after data has been inserted successfully
res.redirect('/login'); res.redirect('/login');
} catch (error) { } catch (error) {
// If the insert fails, redirect to GET /register route // If an error occurs during registration, respond with status 500 and an error message
res.status(400); res.status(500).json({ status: 'error', message: 'An error occurred during registration' });
//res.json({status: 'success', message: 'Invalid input'});
res.redirect('/register');
} }
}); });
/************************ /************************
Home Page Routes Home Page Routes

View File

@@ -1,11 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="./img/premie.jpg"
</body>
</html>