1
1
const express = require ( 'express' ) ;
2
2
const bodyParser = require ( 'body-parser' ) ;
3
3
const cors = require ( 'cors' ) ;
4
- const path = require ( 'path' ) ;
5
4
6
5
const app = express ( ) ;
7
6
const port = process . env . PORT || 3000 ;
@@ -19,7 +18,6 @@ app.use(express.static('public'));
19
18
// Authorized users for editing and deleting wikis
20
19
const authorizedUsers = [ 'kRxZy_kRxZy' , 'MyScratchedAccount' , 'mcgdj' ] ;
21
20
22
- // Middleware to check if the user is authorized to edit or delete a wiki
23
21
const isAuthorized = ( username , wikiOwner ) => {
24
22
return username === wikiOwner || authorizedUsers . includes ( username ) ;
25
23
} ;
@@ -53,7 +51,15 @@ app.get('/api/wikis/:id', (req, res) => {
53
51
// API: Delete a wiki by ID
54
52
app . delete ( '/api/wikis/:id' , ( req , res ) => {
55
53
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
+ }
57
63
58
64
const wikiIndex = wikis . findIndex ( wiki => wiki . id === parseInt ( id ) ) ;
59
65
if ( wikiIndex === - 1 ) {
@@ -62,19 +68,18 @@ app.delete('/api/wikis/:id', (req, res) => {
62
68
63
69
const wiki = wikis [ wikiIndex ] ;
64
70
65
- // Check if the user is authorized to delete
66
71
if ( ! isAuthorized ( username , wiki . owner ) ) {
67
72
return res . status ( 403 ) . json ( { error : 'Unauthorized to delete this wiki' } ) ;
68
73
}
69
74
70
75
wikis . splice ( wikiIndex , 1 ) ;
71
-
72
76
res . json ( { message : 'Wiki deleted successfully' } ) ;
73
77
} ) ;
74
78
75
79
// Serve HTML page for a specific wiki title
76
80
app . get ( '/wiki/:title' , ( req , res ) => {
77
81
const { title } = req . params ;
82
+ const { user } = req . query ;
78
83
const wiki = wikis . find ( w => w . title . toLowerCase ( ) === title . toLowerCase ( ) ) ;
79
84
80
85
if ( ! wiki ) {
@@ -102,7 +107,7 @@ app.get('/wiki/:title', (req, res) => {
102
107
.edit-button:hover { background: #ffaa00; }
103
108
.report-button { background: #ff4d4d; }
104
109
.report-button:hover { background: #ff1a1a; }
105
- .delete-button { background: #d11a2a; }
110
+ .delete-button { background: #d11a2a; display: none; }
106
111
.delete-button:hover { background: #a3001b; }
107
112
</style>
108
113
</head>
@@ -114,7 +119,7 @@ app.get('/wiki/:title', (req, res) => {
114
119
<div class="wiki-content">
115
120
<h2>${ wiki . title } </h2>
116
121
<p>${ wiki . content } </p>
117
- <small>Author: ${ wiki . owner } </small>
122
+ <small id="wiki-owner"> ${ wiki . owner } </small>
118
123
<div class="button-container">
119
124
<a href="https://scratch-coding-hut.github.io/Wiki/edit?edit=${ encodeURIComponent ( wiki . title ) } " class="edit-button">Edit Wiki</a>
120
125
<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) => {
123
128
</div>
124
129
125
130
<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
+
126
137
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
+ }
129
143
130
144
if (!confirm("Are you sure you want to delete this wiki?")) return;
131
145
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"
136
148
})
137
149
.then(response => response.json())
138
150
.then(data => {
@@ -145,6 +157,16 @@ app.get('/wiki/:title', (req, res) => {
145
157
})
146
158
.catch(error => console.error("Error deleting wiki:", error));
147
159
}
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
+ };
148
170
</script>
149
171
</body>
150
172
</html>` ) ;
0 commit comments