mirror of
https://github.com/4ev-link/4ev.link.git
synced 2026-01-13 16:18:05 +00:00
Feat: Endpoint to ban users
This commit is contained in:
19
functions/api/admin/ban-user.js
Normal file
19
functions/api/admin/ban-user.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export async function onRequestPost({ request, env }) {
|
||||
try {
|
||||
const { admin_pass, username, days } = await request.json();
|
||||
if (admin_pass !== env.ADMIN_PASS) return new Response("Unauthorized", { status: 401 });
|
||||
if (!username || !days) return new Response("Missing fields", { status: 400 });
|
||||
|
||||
const bannedUntil = Date.now() + (parseInt(days) * 24 * 60 * 60 * 1000);
|
||||
|
||||
const res = await env.D1_EV.prepare("UPDATE users SET banned_until = ? WHERE username = ?")
|
||||
.bind(bannedUntil, username)
|
||||
.run();
|
||||
|
||||
if (res.meta.changes === 0) return new Response("User not found", { status: 404 });
|
||||
|
||||
return new Response(`User ${username} banned for ${days} days.`);
|
||||
} catch (e) {
|
||||
return new Response(e.message, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user