More app.js reorganization

This commit is contained in:
2024-05-29 01:16:31 -05:00
parent 32fc1ff745
commit 1b106f4385
3 changed files with 19 additions and 121 deletions

View File

@@ -38,13 +38,13 @@ app.use(bodyParser.json()); // specify the usage of JSON for parsing request bod
// <!-- Section 4 : Session Setup -->
// *****************************************************
const sessionConfig = require("./config/session"); // Import the hbs module
sessionConfig(app);
// *****************************************************
// <!-- Section 5 : Website Routes -->
// *****************************************************
const sessionConfig = require("./config/session"); // Import the hbs module
sessionConfig(app);
// *****************************************************
// <!-- Section 4 : Middleware -->
// *****************************************************
@@ -105,18 +105,24 @@ app.get("/club/:clubID", [fetchClubsData], (req, res) => {
});
// *****************************************************
// <!-- Section 5 : API Routes -->
// <!-- Section 5 : Routes -->
// *****************************************************
// Redirect to the home page
app.get("/", (req, res) => {
res.redirect("/home");
});
// Render the home page
app.get("/home", (req, res) => {
const loggedIn = req.session.user ? true : false;
res.render("pages/home");
});
/************************
Login Page Routes
*************************/
// Redirect to the /login endpoint
app.get("/", (req, res) => {
res.redirect("/home");
});
// Account Routes
const loginRoutes = require("./routes/database/login");
app.use("/", loginRoutes);
@@ -125,67 +131,14 @@ app.use("/", loginRoutes);
Registration Page Routes
*************************/
// Render registration page for /register route
app.get("/register", (req, res) => {
res.redirect("/");
});
// 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" });
}
// Check if the username already exists in the database
const existingUser = await db.oneOrNone(
"SELECT * FROM users WHERE username = $1",
req.body.username
);
if (existingUser) {
// If a user with the same username already exists, respond with status 409 and an error message
return res
.status(409)
.json({ status: "error", message: "Username already exists" });
}
// 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,
]);
const user = await db.oneOrNone(
"SELECT * FROM users WHERE username = $1",
req.body.username
);
req.session.user = user;
req.session.save();
// Redirect user to the home page
res.redirect("/home");
} 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",
});
}
});
// Account Routes
const registerRoutes = require("./routes/database/register");
app.use("/", registerRoutes);
/************************
Home Page Routes
*************************/
app.get("/home", (req, res) => {
const loggedIn = req.session.user ? true : false;
res.render("pages/home");
});
app.get("/logout", (req, res) => {
req.session.destroy((err) => {
if (err) {