From 101d32b96062d2210f1701be0565d074f7be62f3 Mon Sep 17 00:00:00 2001 From: tfcprivt <141457761+privt00@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:30:56 +0200 Subject: [PATCH] Update pages.js --- api/handlers/pages.js | 137 ++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 72 deletions(-) diff --git a/api/handlers/pages.js b/api/handlers/pages.js index 240c186..1a850f7 100644 --- a/api/handlers/pages.js +++ b/api/handlers/pages.js @@ -1,89 +1,82 @@ const indexjs = require("../../index.js"); const ejs = require("ejs"); const express = require("express"); +const fetch = require("node-fetch"); const settings = require("../../settings.json"); -const fetch = require('node-fetch'); module.exports.load = async function(app, db) { + app.use('/assets', express.static('./assets')); app.all("/", async (req, res) => { - if (req.session.pterodactyl) if (req.session.pterodactyl.id !== await db.get("users-" + req.session.userinfo.id)) return res.redirect("/login?prompt=none") + if (req.session.pterodactyl && req.session.pterodactyl.id !== await db.get(`users-${req.session.userinfo.id}`)) + return res.redirect("/login?prompt=none"); + let theme = indexjs.get(req); - if (theme.settings.mustbeloggedin.includes(req._parsedUrl.pathname)) if (!req.session.userinfo || !req.session.pterodactyl) return res.redirect("/login"); - if (theme.settings.mustbeadmin.includes(req._parsedUrl.pathname)) { - ejs.renderFile( - `./themes/${theme.name}/${theme.settings.notfound}`, - await eval(indexjs.renderdataeval), - null, - async function (err, str) { - delete req.session.newaccount; - if (!req.session.userinfo || !req.session.pterodactyl) { - if (err) { - console.log(`[WEBSITE] An error has occured on path ${req._parsedUrl.pathname}:`); - console.log(err); - return res.send("An error has occured while attempting to load this page. Please contact an administrator to fix this."); - }; - return res.send(str); - }; + if (theme.settings.mustbeloggedin.includes(req._parsedUrl.pathname) && (!req.session.userinfo || !req.session.pterodactyl)) + return res.redirect("/login"); - let cacheaccount = await fetch( - settings.pterodactyl.domain + "/api/application/users/" + (await db.get("users-" + req.session.userinfo.id)) + "?include=servers", - { - method: "get", - headers: { 'Content-Type': 'application/json', "Authorization": `Bearer ${settings.pterodactyl.key}` } - } - ); - if (await cacheaccount.statusText == "Not Found") { - if (err) { - console.log(`[WEBSITE] An error has occured on path ${req._parsedUrl.pathname}:`); - console.log(err); - return res.send("An error has occured while attempting to load this page. Please contact an administrator to fix this."); - }; - return res.send(str); - }; - let cacheaccountinfo = JSON.parse(await cacheaccount.text()); - - req.session.pterodactyl = cacheaccountinfo.attributes; - if (cacheaccountinfo.attributes.root_admin !== true) { - if (err) { - console.log(`[WEBSITE] An error has occured on path ${req._parsedUrl.pathname}:`); - console.log(err); - return res.send("An error has occured while attempting to load this page. Please contact an administrator to fix this."); - }; - return res.send(str); + if (theme.settings.mustbeadmin.includes(req._parsedUrl.pathname)) { + const renderPage = async (err, str) => { + delete req.session.newaccount; + if (!req.session.userinfo || !req.session.pterodactyl || err) { + console.log(`[WEBSITE] An error has occurred on path ${req._parsedUrl.pathname}:`); + console.log(err); + return res.render("404.ejs", { err }); + } + let cacheAccount = await fetch( + `${settings.pterodactyl.domain}/api/application/users/${(await db.get(`users-${req.session.userinfo.id}`))}?include=servers`, + { + method: "GET", + headers: { 'Content-Type': 'application/json', "Authorization": `Bearer ${settings.pterodactyl.key}` } + } + ); + if (await cacheAccount.statusText == "Not Found") { + return res.send(str); + } + let cacheAccountInfo = JSON.parse(await cacheAccount.text()); + req.session.pterodactyl = cacheAccountInfo.attributes; + if (cacheAccountInfo.attributes.root_admin !== true) { + return res.send(str); + } + ejs.renderFile( + `./themes/${theme.name}/${theme.settings.index}`, + await indexjs.renderdataeval(req), + null, + (err, str) => { + if (err) { + console.log(`[WEBSITE] An error has occurred on path ${req._parsedUrl.pathname}:`); + console.log(err); + return res.render("404.ejs", { err }); + } + delete req.session.newaccount; + res.send(str); + } + ); }; - ejs.renderFile( - `./themes/${theme.name}/${theme.settings.index}`, - await eval(indexjs.renderdataeval), - null, - function (err, str) { - if (err) { - console.log(`[WEBSITE] An error has occured on path ${req._parsedUrl.pathname}:`); - console.log(err); - return res.send("An error has occured while attempting to load this page. Please contact an administrator to fix this."); - }; - delete req.session.newaccount; - res.send(str); - }); - }); - return; - }; + `./themes/${theme.name}/${theme.settings.notfound}`, + await indexjs.renderdataeval(req), + null, + renderPage + ); + return; + } ejs.renderFile( - `./themes/${theme.name}/${theme.settings.index}`, - await eval(indexjs.renderdataeval), - null, - function (err, str) { - if (err) { - console.log(`[WEBSITE] An error has occured on path ${req._parsedUrl.pathname}:`); - console.log(err); - return res.send("An error has occured while attempting to load this page. Please contact an administrator to fix this."); - }; - delete req.session.newaccount; - res.send(str); - }); + `./themes/${theme.name}/${theme.settings.index}`, + await indexjs.renderdataeval(req), + null, + (err, str) => { + if (err) { + console.log(`[WEBSITE] An error has occurred on path ${req._parsedUrl.pathname}:`); + console.log(err); + return res.render("404.ejs", { err }); + } + delete req.session.newaccount; + res.send(str); + } + ); }); app.use('/assets', express.static('./assets')); app.use('/cdn', express.static('./cdn')); -}; \ No newline at end of file +};