Fix to index.js to fix code differences after merge
This commit is contained in:
@@ -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) => {
|
|
||||||
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
|
// Trigger Registration Form to Post
|
||||||
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
|
app.post('/register', async (req, res) => {
|
||||||
//res.status(200);
|
try {
|
||||||
res.json({status: 'success', message: 'Success'});
|
if (!req.body.username || !req.body.password) {
|
||||||
// Direct user to login screen after data has been inserted successfully
|
// If username or password is missing, respond with status 400 and an error message
|
||||||
res.redirect('/login');
|
return res.status(400).json({ status: 'error', message: 'Invalid input' });
|
||||||
} catch (error) {
|
|
||||||
// If the insert fails, redirect to GET /register route
|
|
||||||
res.status(400);
|
|
||||||
//res.json({status: 'success', message: 'Invalid input'});
|
|
||||||
res.redirect('/register');
|
|
||||||
}
|
}
|
||||||
});
|
// 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
|
Home Page Routes
|
||||||
|
|||||||
@@ -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>
|
|
||||||
Reference in New Issue
Block a user