From 942ad7add0dd574384441f063bebd15fd3551d89 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Mon, 12 May 2025 15:48:29 -0700 Subject: [PATCH] assignment 2 --- .gitignore | 1 - index.js | 61 ++++++++++++++++++++++++++++++++++----- public/css/index.css | 47 ------------------------------ views/admin.ejs | 53 ++++++++++++++++++++++++++++++++++ views/index.ejs | 40 ++++++++++++++++++------- views/login.ejs | 6 ++-- views/members.ejs | 26 ++++++++++++----- views/notFound.ejs | 15 ++++++++-- views/partials/footer.ejs | 10 +++++++ views/partials/header.ejs | 14 +++++++++ views/signup.ejs | 5 +++- 11 files changed, 199 insertions(+), 79 deletions(-) delete mode 100644 public/css/index.css create mode 100644 views/admin.ejs diff --git a/.gitignore b/.gitignore index ecff155..1945131 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules/ package-lock.json .env -*.sync-confcict-* \ No newline at end of file diff --git a/index.js b/index.js index 2601f60..a1c70a5 100755 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const MongoStore = require("connect-mongo"); const MongoClient = require("mongodb").MongoClient; const joi = require("joi"); const bcrypt = require("bcrypt"); +const ObjectId = require("mongodb").ObjectId; // Initialize app const app = express(); @@ -19,6 +20,7 @@ async function connectMongo() { try { const connection = await MongoClient.connect(mongoURI, { connectTimeoutMS: 1000 }); users = connection.db(config.mongo_database).collection("users"); + console.log("Connected to mongo"); } catch (err) { console.error(`Failed to connect to mongodb at (mongodb+srv://${config.mongo_host})`); } @@ -35,7 +37,7 @@ app.use(session({ store: MongoStore.create({ mongoUrl: `${mongoURI}/${config.mongo_database}`, crypto: { secret: config.mongo_secret } }), resave: true, saveUninitialized: false, - cookie: { maxAge: 60000 }, + cookie: { maxAge: 3600000 }, })); /**** Page routes ****/ @@ -43,19 +45,19 @@ app.use(session({ app.get("/", async (req, res) => { res.set('Content-Type', 'text/html'); - res.render("index", { authenticated: req.session.authenticated, username: req.session.username }); + res.render("index", { authenticated: req.session.authenticated, user: req.session.user }); return res.status(200); }); app.get("/login", (req, res) => { res.set('Content-Type', 'text/html'); - res.render("login", { message: req.session.message }); + res.render("login", { authenticated: req.session.authenticated, message: req.session.message }); return res.status(200); }) app.get("/signup", (req, res) => { res.set('Content-Type', 'text/html'); - res.render("signup", { message: req.session.message }); + res.render("signup", { authenticated: req.session.authenticated, message: req.session.message }); return res.status(200); }); @@ -70,10 +72,29 @@ app.get("/members", (req, res) => { let name = names[Math.floor(Math.random() * names.length)]; res.set('Content-Type', 'text/html'); - res.render("members", { name: name, username: req.session.username }); + res.render("members", { authenticated: req.session.authenticated, user: req.session.user }); return res.status(200); }); +app.get("/admin", async (req, res) => { + if (!req.session.authenticated) { + req.session.message = "Please login"; + res.status(401); + return res.redirect("/login"); + } + + if (req.session.user.type != "admin") { + req.session.message = "You are not authorized to access that resource"; + res.status(401); + return res.redirect("/members"); + } + + let userList = await users.find({}).toArray(); + + res.set('Content-Type', 'text/html'); + res.render("admin", { authenticated: req.session.authenticated, user: req.session.user, users: userList }); +}); + /*** Authentication routes ***/ app.post("/login", async (req, res) => { @@ -96,7 +117,7 @@ app.post("/login", async (req, res) => { if (await bcrypt.compare(req.body.password, user.password)) { req.session.authenticated = true; - req.session.username = user.name; + req.session.user = user; req.session.message = ""; res.status(200); @@ -128,10 +149,16 @@ app.post("/signup", async (req, res) => { name: req.body.name, email: req.body.email, password: password, + type: "user" }); req.session.authenticated = true; - req.session.username = req.body.name; + req.session.user = { + name: req.body.name, + email: req.body.email, + password: password, // bad but oh well + type: "user" + }; req.session.message = ""; res.status(200); @@ -144,11 +171,29 @@ app.get("/logout", (req, res) => { return res.redirect('/'); }); +app.post("/promote", (req, res) => { + users.updateOne({ _id: new ObjectId(req.body.userId) }, { $set: { type: "admin" } }).then((results) => { + return res.redirect("/admin"); + }).catch((err) => { + console.error(err); + return res.redirect("/admin"); + }); +}); + +app.post("/demote", (req, res) => { + users.updateOne({ _id: new ObjectId(req.body.userId) }, { $set: { type: "user" } }).then((results) => { + return res.redirect("/admin"); + }).catch((err) => { + console.error(err); + return res.redirect("/admin"); + }); +}); + /*** 404 Not found ***/ app.get("/*splat", (req, res) => { res.set('Content-Type', 'text/html'); - res.render("notFound"); + res.render("notFound", { authenticated: req.session.authenticated }); return res.status(404); }); diff --git a/public/css/index.css b/public/css/index.css deleted file mode 100644 index 529137a..0000000 --- a/public/css/index.css +++ /dev/null @@ -1,47 +0,0 @@ -@import url('https://fonts.googleapis.com/css2?family=Oswald&display=swap'); - -* { - margin: 0pt; - padding: 0pt; - box-sizing: border-box; - font-family: 'Oswald', sans-serif; - overflow-x: hidden; - overflow-y: hidden; -} - -body { - width: 100vw; - height: 100vh; -} - -.center { - position: absolute; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - text-align: center; -} - -.center p { - margin: 12pt; -} - -.center a { - padding-left: 6pt; - padding-right: 6pt; - margin-top: 2pt; - margin-bottom: 2pt; - font-size: 24px; - background-color: lightsteelblue; - border-radius: 8px; -} - -.center form { - display: flex; - flex-direction: column; - justify-content: center; -} - -.flash-msg { - color: red; -} \ No newline at end of file diff --git a/views/admin.ejs b/views/admin.ejs new file mode 100644 index 0000000..d460f90 --- /dev/null +++ b/views/admin.ejs @@ -0,0 +1,53 @@ +<%- include("./partials/header") %> + +
+
+ +
+

Welcome <%= user.name %>

+ Logout +
+ +
+ +

Users

+
+ <% users.forEach((u) => { %> +
+

<%= u.type %>

+

<%= u.name %>

+

<%= u.email %>

+
+ <% if (u._id == user._id) { %> +
+ + +
+ <% } else { %> + <% if (u.type == "admin") { %> +
+ + +
+ <% } else { %> +
+ + +
+ <% } %> + <% } %> +
+
+ <% }) %> +
+
+
+ +<%- include("./partials/footer") %> + \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index d837585..5502567 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,15 +1,33 @@ <%- include("./partials/header") %> -
- <% if (authenticated) { %> -

Welcome <%= username %>

- Go to members page - Logout - <% } else { %> - Login -

or

- Signup - <% } %> -
+<% if (authenticated) { %> +
+
+ +
+

Welcome <%= user.name %>

+ Logout +
+ +
+ Go to members page + <% if (user.type == "admin") { %> + Go to admin page + <% } %> +
+
+
+<% } else { %> +
+
+ + Login +

or

+ Signup + +
+
+ +<% } %> <%- include("./partials/footer") %> diff --git a/views/login.ejs b/views/login.ejs index 6befa5b..e0519d3 100755 --- a/views/login.ejs +++ b/views/login.ejs @@ -1,12 +1,15 @@ <%- include("./partials/header") %>
-
+

Login

+ <% if (message) { %> +

<%= message %>

+ <% } %> @@ -20,7 +23,6 @@
-

<%= message %>

diff --git a/views/members.ejs b/views/members.ejs index 2be4b64..f2bceb0 100644 --- a/views/members.ejs +++ b/views/members.ejs @@ -1,11 +1,23 @@ <%- include("./partials/header") %> -

Hello <%= username %>

-
- -
<%= name %>
-
-
-Logout +
+
+ +
+

Welcome <%= user.name %>

+ <% if (user.type == "admin") { %> + Admin + <% } %> + Logout +
+ +
+ + + +
+ +
+
<%- include("./partials/footer") %> diff --git a/views/notFound.ejs b/views/notFound.ejs index 79027cf..5fdb341 100755 --- a/views/notFound.ejs +++ b/views/notFound.ejs @@ -1,6 +1,17 @@ <%- include("./partials/header") %> -

404 - Not found

-Home +
+

404 - Not found

+ Home +
+ +
+
+ +

404 - Not found

+ Home + +
+
<%- include("./partials/footer") %> diff --git a/views/partials/footer.ejs b/views/partials/footer.ejs index b605728..b87da19 100644 --- a/views/partials/footer.ejs +++ b/views/partials/footer.ejs @@ -1,2 +1,12 @@ + +
+
+
+
Copyright 2025
+
Super duper cool app
+
+
+ +
diff --git a/views/partials/header.ejs b/views/partials/header.ejs index 2948d7e..09e33ba 100644 --- a/views/partials/header.ejs +++ b/views/partials/header.ejs @@ -8,3 +8,17 @@ + diff --git a/views/signup.ejs b/views/signup.ejs index dd5c898..ae18b4b 100755 --- a/views/signup.ejs +++ b/views/signup.ejs @@ -8,6 +8,10 @@
+ <% if (message) { %> +

<%= message %>

+ <% } %> + @@ -26,7 +30,6 @@
-

<%= message %>