Updated to separate login routes from app.js

This commit is contained in:
2024-05-28 20:42:45 -05:00
parent ebb558542c
commit a3ec0ca01d
4 changed files with 105 additions and 40 deletions

View File

@@ -147,45 +147,9 @@ app.get("/", (req, res) => {
res.redirect("/home");
});
// Render login page for /login route
app.get("/login", (req, res) => {
res.render("/");
});
// Trigger login form to check database for matching username and password
app.post("/login", async (req, res) => {
try {
// Check if username exists in DB
const user = await db.oneOrNone(
"SELECT * FROM users WHERE username = $1",
req.body.username
);
if (!user) {
// Redirect user to login screen if no user is found with the provided username
return res.redirect("/register");
}
// Check if password from request matches with password in DB
const match = await bcrypt.compare(req.body.password, user.password);
// Check if match returns no data
if (!match) {
// Render the login page with the message parameter
return res.render("/", { message: "Password does not match" });
} else {
// Save user information in the session variable
req.session.user = user;
req.session.save();
// Redirect user to the home page
res.redirect("/");
}
} catch (error) {
// Direct user to login screen if no user is found with matching password
res.redirect("/register");
}
});
// Account Routes
const loginRoutes = require("./routes/database/login");
app.use("/", loginRoutes);
/************************
Registration Page Routes