added session storage to application

This commit is contained in:
2025-07-07 22:56:04 +02:00
parent dda29ac15c
commit 97e749630e
53 changed files with 5853 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ import {
import dotenv from "dotenv";
import path from "path";
import axios from "axios";
import session from "express-session";
import { fileURLToPath } from "url";
const app = express();
dotenv.config();
@@ -21,6 +22,15 @@ app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(
session({
secret: "p0wP3asqAx1Ab0",
resave: false,
saveUninitialized: false,
cookie: { secure: false }, // Set to true if using HTTPS
})
);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -33,17 +43,26 @@ app.get("/", (req, res) => {
res.render("index.ejs", { error: null });
});
// login code
app.post("/login", async (req, res) => {
const { username, password } = req.body;
loginUser(username, password).then((result) => {
if (result.success === true) {
res.render("userView.ejs");
req.session.user = result.user;
res.render("userView.ejs", { user: result.user });
} else {
res.render("index.ejs", { error: result.message });
}
});
});
// logout code
app.get("/logout", (req, res) => {
req.session.destroy(() => {
res.redirect("/");
});
});
// error handling code
app.use((err, req, res, next) => {
// Log the error stack and send a generic error response