From 16d538bd05fdff20bb7bb259d552af65a6bc3050 Mon Sep 17 00:00:00 2001 From: andrewstech Date: Wed, 3 Apr 2024 14:20:08 +0000 Subject: [PATCH] Bug fixes and start work on edit page --- routes/domains.js | 23 ++++++- routes/edit.js | 25 ++++++++ util/router.js | 4 ++ util/routes.js | 4 +- views/domain.ejs | 18 ++++-- views/edit.ejs | 158 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 223 insertions(+), 9 deletions(-) create mode 100644 routes/edit.js create mode 100644 views/edit.ejs diff --git a/routes/domains.js b/routes/domains.js index a4e0676..517fe14 100644 --- a/routes/domains.js +++ b/routes/domains.js @@ -1,7 +1,10 @@ +const e = require("express"); + const ENDPOINT = process.env.ENDPOINT; module.exports = async (req, res) => { const user = req.user; const token = req.cookies.token; + const code = req.query.code; let data = await fetch(ENDPOINT + `/domains` , { method: 'GET', headers: { @@ -10,6 +13,24 @@ module.exports = async (req, res) => { }, }); data = await data.json(); - res.render("domain", {user: user, domains: data}) + + if (code){ + if (code == 1){ + return res.render("domain", {user: user, domains: data, message: "System error, please try again later"}); + } + if (code == 2){ + return res.render("domain", {user: user, domains: data, message: "Domain not found"}); + } + if (code == 3){ + return res.render("domain", {user: user, domains: data, message: "Domain already exists"}); + } + if (code == 4){ + return res.render("domain", {user: user, domains: data, message: "You do not have permission to edit this domain"}); + } + } else { + return res.render("domain", {user: user, domains: data, message: ""}); + } + + } \ No newline at end of file diff --git a/routes/edit.js b/routes/edit.js new file mode 100644 index 0000000..13e0718 --- /dev/null +++ b/routes/edit.js @@ -0,0 +1,25 @@ +const ENDPOINT = process.env.ENDPOINT; +module.exports = async (req, res) => { + const domain = req.query.domain; + const user = req.user; + const token = req.cookies.token; + let data = await fetch(ENDPOINT + `/domains` , { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `${token}` + }, + }); + data = await data.json(); + // check if user is the owner of the domain + let owner = false; + for (let i = 0; i < data.length; i++){ + if (data[i]._id == domain){ + owner = true; + } + } + if (!owner){ + return res.redirect("/domains?code=4"); + } + res.render("edit", {user: user, domain: domain, message: ""}); +} diff --git a/util/router.js b/util/router.js index 63326ed..cd551d4 100644 --- a/util/router.js +++ b/util/router.js @@ -63,5 +63,9 @@ router.post("/password", authenticateToken, upload.any(), (req, res) => { routes.password(req, res); }); +router.get("/edit/", authenticateToken, (req, res) => { + routes.edit(req, res); +}); + module.exports = router; \ No newline at end of file diff --git a/util/routes.js b/util/routes.js index 1a03d48..e2049ed 100644 --- a/util/routes.js +++ b/util/routes.js @@ -8,6 +8,7 @@ const logout = require("../routes/logout"); const staff = require("../routes/staff"); const mfa = require("../routes/mfa"); const password = require("../routes/password"); +const edit = require("../routes/edit"); module.exports = { @@ -20,5 +21,6 @@ module.exports = { logout, staff, mfa, - password + password, + edit } \ No newline at end of file diff --git a/views/domain.ejs b/views/domain.ejs index 989cab2..63a3ced 100644 --- a/views/domain.ejs +++ b/views/domain.ejs @@ -59,8 +59,7 @@ --> @@ -90,7 +89,7 @@ My domains <% if (user.staffMember) { %> - Staff + Staff <% } %> Donate @@ -112,14 +111,19 @@
- Your Profile - Settings + Settings Sign out
- + + <% if (message && message.length > 0) { %> + + <% } %>

Hello <%= user.username %>

@@ -147,7 +151,7 @@ <%= domain._id %> <%= domain.status %> - Edit + Edit <% }) %> diff --git a/views/edit.ejs b/views/edit.ejs new file mode 100644 index 0000000..5504a1c --- /dev/null +++ b/views/edit.ejs @@ -0,0 +1,158 @@ + + + + + + + +
+ + + <% if (message && message.length > 0) { %> + + <% } %> +
+
+

Editing <%= domain %>

+
+
+
+
+ + + +
+
+
+ \ No newline at end of file