-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.js
More file actions
216 lines (174 loc) · 6.15 KB
/
admin.js
File metadata and controls
216 lines (174 loc) · 6.15 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// admin.js
import { renderAddUserForm, renderAddPostForm, renderEditPostForm } from './templates.js';
import { hashPassword } from './auth.js';
import { verifyJWT } from './jwt.js';
import { parseCookies } from './utils.js';
export async function addUser(env, username, password) {
try {
const { hash, salt } = await hashPassword(password);
await env.DB.prepare(
`INSERT INTO users (username, password, salt) VALUES (?, ?, ?)`
)
.bind(username, hash, salt)
.run();
return new Response(`User ${username} added successfully!`, {
headers: { 'Content-Type': 'text/html' },
});
} catch (error) {
return new Response(`Error adding user: ${error.message}`, {
status: 500,
});
}
}
export async function handleAdminRequest(request, env) {
const url = new URL(request.url);
const pathname = url.pathname;
const method = request.method;
// Route to add a new post (GET)
if (pathname === '/admin/add' && method === 'GET') {
return new Response(renderAddPostForm(), {
headers: { 'Content-Type': 'text/html' },
});
}
// Route to add a new post (POST)
if (pathname === '/admin/add' && method === 'POST') {
try {
// Parse cookies and verify JWT
const cookies = parseCookies(request);
const token = cookies.token;
if (!token) {
return new Response('Unauthorized', { status: 401 });
}
const user = await verifyJWT(token, env.JWT_SECRET);
if (!user) {
return new Response('Unauthorized', { status: 401 });
}
const userId = user.id; // Extract userId from JWT payload
console.log('User ID:', userId);
// Get form data
const formData = await request.formData();
const title = formData.get('title');
const content = formData.get('content');
// Validate data
if (!title || !content || !userId) {
console.error('Missing data:', { title, content, userId });
return new Response('Missing data', { status: 400 });
}
// Insert the new post into the database with user_id
await env.DB.prepare(
`INSERT INTO posts (title, content, user_id) VALUES (?, ?, ?)`
)
.bind(title, content, userId)
.run();
// Redirect back to the homepage
return Response.redirect(url.origin + '/', 303);
} catch (error) {
console.error('Error adding post:', error);
return new Response(`Error adding post: ${error.message}`, {
status: 500,
});
}
}
// Route to edit a post (GET)
if (pathname.startsWith('/admin/edit/') && method === 'GET') {
const postId = pathname.split('/').pop();
// Fetch the post from the database
const postResult = await env.DB.prepare(
`SELECT * FROM posts WHERE id = ?`
)
.bind(postId)
.first();
if (!postResult) {
return new Response('Post not found', { status: 404 });
}
// Render the edit post form
return new Response(renderEditPostForm(postResult), {
headers: { 'Content-Type': 'text/html' },
});
}
// Route to edit a post (POST)
if (pathname.startsWith('/admin/edit/') && method === 'POST') {
try {
// Parse cookies and verify JWT
const cookies = parseCookies(request);
const token = cookies.token;
if (!token) {
return new Response('Unauthorized', { status: 401 });
}
const user = await verifyJWT(token, env.JWT_SECRET);
if (!user) {
return new Response('Unauthorized', { status: 401 });
}
const postId = pathname.split('/').pop();
// Get form data
const formData = await request.formData();
const title = formData.get('title');
const content = formData.get('content');
// Update the post in the database
await env.DB.prepare(
`UPDATE posts SET title = ?, content = ? WHERE id = ?`
)
.bind(title, content, postId)
.run();
return Response.redirect(url.origin + '/', 303);
} catch (error) {
return new Response(`Error updating post: ${error.message}`, {
status: 500,
});
}
}
// Route to delete a post
if (pathname.startsWith('/admin/delete/') && method === 'POST') {
try {
// Parse cookies and verify JWT
const cookies = parseCookies(request);
const token = cookies.token;
if (!token) {
return new Response('Unauthorized', { status: 401 });
}
const user = await verifyJWT(token, env.JWT_SECRET);
if (!user) {
return new Response('Unauthorized', { status: 401 });
}
const postId = pathname.split('/').pop();
// Delete the post from the database
await env.DB.prepare(`DELETE FROM posts WHERE id = ?`)
.bind(postId)
.run();
return Response.redirect(url.origin + '/', 303);
} catch (error) {
return new Response(`Error deleting post: ${error.message}`, {
status: 500,
});
}
}
// Route to add a new user (GET)
if (pathname === '/admin/add-user' && method === 'GET') {
// ADD THESE LINES:
const cookies = parseCookies(request);
const token = cookies.token;
if (!token) return new Response('Unauthorized', { status: 401 });
const user = await verifyJWT(token, env.JWT_SECRET);
if (!user) return new Response('Unauthorized', { status: 401 });
return new Response(renderAddUserForm(), {
headers: { 'Content-Type': 'text/html' },
});
}
// Route to add a new user (POST)
if (pathname === '/admin/add-user' && method === 'POST') {
// ADD AUTH CHECK HERE TOO
const cookies = parseCookies(request);
const token = cookies.token;
if (!token) return new Response('Unauthorized', { status: 401 });
const user = await verifyJWT(token, env.JWT_SECRET);
if (!user) return new Response('Unauthorized', { status: 401 });
const formData = await request.formData();
const username = formData.get('username');
const password = formData.get('password');
// Also add redirect after user creation:
await addUser(env, username, password);
return Response.redirect(url.origin + '/', 303);
}
// Default response for unknown routes
return new Response('Not found', { status: 404 });
}