More app.js reorganization
This commit is contained in:
83
src/app.js
83
src/app.js
@@ -38,13 +38,13 @@ app.use(bodyParser.json()); // specify the usage of JSON for parsing request bod
|
|||||||
// <!-- Section 4 : Session Setup -->
|
// <!-- Section 4 : Session Setup -->
|
||||||
// *****************************************************
|
// *****************************************************
|
||||||
|
|
||||||
|
const sessionConfig = require("./config/session"); // Import the hbs module
|
||||||
|
sessionConfig(app);
|
||||||
|
|
||||||
// *****************************************************
|
// *****************************************************
|
||||||
// <!-- Section 5 : Website Routes -->
|
// <!-- Section 5 : Website Routes -->
|
||||||
// *****************************************************
|
// *****************************************************
|
||||||
|
|
||||||
const sessionConfig = require("./config/session"); // Import the hbs module
|
|
||||||
sessionConfig(app);
|
|
||||||
|
|
||||||
// *****************************************************
|
// *****************************************************
|
||||||
// <!-- Section 4 : Middleware -->
|
// <!-- 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
|
Login Page Routes
|
||||||
*************************/
|
*************************/
|
||||||
|
|
||||||
// Redirect to the /login endpoint
|
|
||||||
app.get("/", (req, res) => {
|
|
||||||
res.redirect("/home");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Account Routes
|
// Account Routes
|
||||||
const loginRoutes = require("./routes/database/login");
|
const loginRoutes = require("./routes/database/login");
|
||||||
app.use("/", loginRoutes);
|
app.use("/", loginRoutes);
|
||||||
@@ -125,67 +131,14 @@ app.use("/", loginRoutes);
|
|||||||
Registration Page Routes
|
Registration Page Routes
|
||||||
*************************/
|
*************************/
|
||||||
|
|
||||||
// Render registration page for /register route
|
// Account Routes
|
||||||
app.get("/register", (req, res) => {
|
const registerRoutes = require("./routes/database/register");
|
||||||
res.redirect("/");
|
app.use("/", registerRoutes);
|
||||||
});
|
|
||||||
|
|
||||||
// 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",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/************************
|
/************************
|
||||||
Home Page Routes
|
Home Page Routes
|
||||||
*************************/
|
*************************/
|
||||||
|
|
||||||
app.get("/home", (req, res) => {
|
|
||||||
const loggedIn = req.session.user ? true : false;
|
|
||||||
res.render("pages/home");
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/logout", (req, res) => {
|
app.get("/logout", (req, res) => {
|
||||||
req.session.destroy((err) => {
|
req.session.destroy((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const path = require("path");
|
const db = require("../../database/db"); // Import the db modulei
|
||||||
const handlebars = require("express-handlebars");
|
|
||||||
const Handlebars = require("handlebars");
|
|
||||||
const bodyParser = require("body-parser");
|
|
||||||
const bcrypt = require("bcryptjs"); // To hash passwords
|
const bcrypt = require("bcryptjs"); // To hash passwords
|
||||||
const session = require("express-session"); // To set the session object. To store or access session data, use the `req.session`, which is (generally) serialized as JSON by the store.
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// Trigger Registration Form to Post
|
// Trigger Registration Form to Post
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
const express = require("express");
|
|
||||||
const router = express.Router();
|
|
||||||
const bcrypt = require("bcryptjs");
|
|
||||||
const db = require("../config/database");
|
|
||||||
|
|
||||||
// Redirect to the /login endpoint
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = loginRoutes;
|
|
||||||
Reference in New Issue
Block a user