-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadmin.js
More file actions
executable file
·165 lines (145 loc) · 4.4 KB
/
Copy pathadmin.js
File metadata and controls
executable file
·165 lines (145 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const ejs = require("ejs");
const express = require("express");
require("dotenv").config();
const fetch = require("node-fetch");
const cookieParser = require("cookie-parser");
const jwt = require("jsonwebtoken");
const multer = require("multer");;
const path = require("path");
const db = require("./db");
const FormData = require("form-data");
var router = express.Router();
router.use(cookieParser());
router.use(express.json());
router.use(express.urlencoded({ extended: true }));
const checkAuthMiddleware = (req, res, next) => {
const token = req.cookies.auth;
req.jwt = token;
res.locals.jwt = token;
res.locals.url = process.env.URL_ENTIRE;
res.locals.message = req.query.message || res.locals.message || undefined;
res.locals.ip = process.env.SERVER_IP;
res.locals.dns = process.env.DNS_URL;
if (!token) {
res.locals.loggedIn = false;
return next();
}
jwt.verify(token, process.env.AUTH_SECRET, async (err, decoded) => {
if (err) {
res.locals.loggedIn = false;
} else {
var user = await db.findUserById(await decoded.id);
if (!user) {
res.clearCookie("auth");
res.redirect("/?message=Invalid auth cookie");
} else {
res.locals.loggedIn = true;
res.locals.user = await user;
req.user = await user;
}
}
next();
});
};
const notLoggedInMiddleware = (req, res, next) => {
if (res.locals.loggedIn) {
res.redirect("/");
} else {
next();
}
};
const loggedInMiddleware = (req, res, next) => {
if (!res.locals.loggedIn) {
res.redirect("/auth/login?message=You need to be logged in");
} else {
next();
}
}
const isAdmin = (req, res, next) => {
if (req.path == '/auth/login' || req.path == '/') {
return next(); // Skip middleware for the login route
}
if (req.user) {
if (req.user.admin == 1) {
next();
} else {
res.clearCookie("auth");
return res.redirect("/auth/login?message=You need to be an admin");
}
} else {
res.clearCookie("auth");
return res.redirect("/auth/login?message=You need to log in");
}
};
router.use(checkAuthMiddleware);
router.use(isAdmin);
router.get("/", loggedInMiddleware, async (req, res) => {
res.render("adminIndex", await { title: "Index", users: await db.readUsers() });
});
router.get("/auth/login", notLoggedInMiddleware, (req, res) => {
res.render("adminLogin", { title: "Login" });
});
router.post("/auth/login", notLoggedInMiddleware, async (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
res.status(400).json({ error: "Missing required fields", success: false });
return;
} else {
try {
const response = await fetch(process.env.API_URL + "auth/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user: email,
password: password,
}),
timeout: 5000,
});
const text = await response.text();
const data = await JSON.parse(await text);
if (await data.success) {
var user = await db.findUserByEmail(email);
if (user.admin == 1) {
res.cookie("auth", data.jwt, { httpOnly: true });
res.locals.loggedIn = true;
return res.redirect("/?message=Successfully logged in");
} else {
return res.redirect('/?message=Not an admin :P')
}
} else {
res.clearCookie("auth");
res.locals.loggedIn = false;
return res.redirect("/?message=Error "+data.error);
}
} catch (error) {
console.error("Error:", error);
return res.status(500).json({ error: "An error occurred; " + error });
}
}
});
router.get("/auth/logout", (req, res) => {
res.clearCookie("auth");
res.redirect("/");
});
router.post("/user/deleteUser", async (req, res) => {
const { token } = req.body;
if (!token) {
return res.status(403).json({error: "An error occurred", success: false})
}
response = await fetch(process.env.API_URL + "auth/removeAccount", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jwt: token,
})
});
res.redirect("/?message=" + JSON.stringify(await response.json()));
});
router.use((req, res, next) => {
res.status(404).send("404 not found - " + process.env.ADMIN_URL);
});
module.exports = router;