Skip to content

Commit f0b3d8a

Browse files
authored
Update server.js
1 parent 6155eea commit f0b3d8a

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

src/npm/server.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const express = require('express');
22
const bodyParser = require('body-parser');
33
const cors = require('cors');
4-
const path = require('path');
54

65
const app = express();
76
const port = process.env.PORT || 3000;
@@ -19,7 +18,6 @@ app.use(express.static('public'));
1918
// Authorized users for editing and deleting wikis
2019
const authorizedUsers = ['kRxZy_kRxZy', 'MyScratchedAccount', 'mcgdj'];
2120

22-
// Middleware to check if the user is authorized to edit or delete a wiki
2321
const isAuthorized = (username, wikiOwner) => {
2422
return username === wikiOwner || authorizedUsers.includes(username);
2523
};
@@ -53,7 +51,15 @@ app.get('/api/wikis/:id', (req, res) => {
5351
// API: Delete a wiki by ID
5452
app.delete('/api/wikis/:id', (req, res) => {
5553
const { id } = req.params;
56-
const { username } = req.body; // Assume the username is sent in the request body
54+
const { user } = req.query;
55+
if (!user) return res.status(400).json({ error: 'User parameter is required' });
56+
57+
let username;
58+
try {
59+
username = atob(user);
60+
} catch (error) {
61+
return res.status(400).json({ error: 'Invalid username encoding' });
62+
}
5763

5864
const wikiIndex = wikis.findIndex(wiki => wiki.id === parseInt(id));
5965
if (wikiIndex === -1) {
@@ -62,19 +68,18 @@ app.delete('/api/wikis/:id', (req, res) => {
6268

6369
const wiki = wikis[wikiIndex];
6470

65-
// Check if the user is authorized to delete
6671
if (!isAuthorized(username, wiki.owner)) {
6772
return res.status(403).json({ error: 'Unauthorized to delete this wiki' });
6873
}
6974

7075
wikis.splice(wikiIndex, 1);
71-
7276
res.json({ message: 'Wiki deleted successfully' });
7377
});
7478

7579
// Serve HTML page for a specific wiki title
7680
app.get('/wiki/:title', (req, res) => {
7781
const { title } = req.params;
82+
const { user } = req.query;
7883
const wiki = wikis.find(w => w.title.toLowerCase() === title.toLowerCase());
7984

8085
if (!wiki) {
@@ -102,7 +107,7 @@ app.get('/wiki/:title', (req, res) => {
102107
.edit-button:hover { background: #ffaa00; }
103108
.report-button { background: #ff4d4d; }
104109
.report-button:hover { background: #ff1a1a; }
105-
.delete-button { background: #d11a2a; }
110+
.delete-button { background: #d11a2a; display: none; }
106111
.delete-button:hover { background: #a3001b; }
107112
</style>
108113
</head>
@@ -114,7 +119,7 @@ app.get('/wiki/:title', (req, res) => {
114119
<div class="wiki-content">
115120
<h2>${wiki.title}</h2>
116121
<p>${wiki.content}</p>
117-
<small>Author: ${wiki.owner}</small>
122+
<small id="wiki-owner">${wiki.owner}</small>
118123
<div class="button-container">
119124
<a href="https://scratch-coding-hut.github.io/Wiki/edit?edit=${encodeURIComponent(wiki.title)}" class="edit-button">Edit Wiki</a>
120125
<a href="https://scratch-coding-hut.github.io/Wiki/report.html?wiki=${encodeURIComponent(wiki.title)}" class="report-button">Report</a>
@@ -123,16 +128,23 @@ app.get('/wiki/:title', (req, res) => {
123128
</div>
124129
125130
<script>
131+
function getUsernameFromURL() {
132+
const urlParams = new URLSearchParams(window.location.search);
133+
const encodedUser = urlParams.get("user");
134+
return encodedUser ? atob(encodedUser) : null;
135+
}
136+
126137
function deleteWiki(wikiId) {
127-
const username = prompt("Enter your username to confirm deletion:");
128-
if (!username) return alert("Deletion cancelled.");
138+
const username = getUsernameFromURL();
139+
if (!username) {
140+
alert("No user detected. Please log in.");
141+
return;
142+
}
129143
130144
if (!confirm("Are you sure you want to delete this wiki?")) return;
131145
132-
fetch(\`/api/wikis/\${wikiId}\`, {
133-
method: "DELETE",
134-
headers: { "Content-Type": "application/json" },
135-
body: JSON.stringify({ username })
146+
fetch(\`/api/wikis/\${wikiId}?user=\${btoa(username)}\`, {
147+
method: "DELETE"
136148
})
137149
.then(response => response.json())
138150
.then(data => {
@@ -145,6 +157,16 @@ app.get('/wiki/:title', (req, res) => {
145157
})
146158
.catch(error => console.error("Error deleting wiki:", error));
147159
}
160+
161+
window.onload = function() {
162+
const username = getUsernameFromURL();
163+
const wikiOwner = document.getElementById("wiki-owner").textContent;
164+
const authorizedUsers = ["kRxZy_kRxZy", "MyScratchedAccount", "mcgdj"];
165+
166+
if (username && (username === wikiOwner || authorizedUsers.includes(username))) {
167+
document.querySelector(".delete-button").style.display = "inline-block";
168+
}
169+
};
148170
</script>
149171
</body>
150172
</html>`);

0 commit comments

Comments
 (0)