mirror of
https://github.com/4ev-link/4ev.link.git
synced 2026-01-13 16:18:05 +00:00
Feat: Add API endpoint for deleting links
This commit is contained in:
28
functions/api/links/delete.js
Normal file
28
functions/api/links/delete.js
Normal file
@@ -0,0 +1,28 @@
|
||||
export async function onRequestPost({ request, env }) {
|
||||
try {
|
||||
const { 'g-recaptcha-response': token, ...body } = await request.json();
|
||||
const vR = await fetch("https://www.google.com/recaptcha/api/siteverify", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: `secret=${env.RECAPCHA_KEY}&response=${token}` });
|
||||
if (!(await vR.json()).success) return new Response("CAPTCHA verification failed.", { status: 403 });
|
||||
|
||||
const { slug, username, pass_hash } = body;
|
||||
if (!slug || !username || !pass_hash) return new Response("Missing fields", { status: 400 });
|
||||
|
||||
const user = await env.D1_EV.prepare("SELECT pass_hash, custom_slugs FROM users WHERE username = ?").bind(username).first();
|
||||
if (user?.pass_hash !== pass_hash) return new Response("Invalid credentials", { status: 401 });
|
||||
|
||||
let slugs = [];
|
||||
try { slugs = JSON.parse(user.custom_slugs) } catch {}
|
||||
if (!Array.isArray(slugs) || !slugs.includes(slug)) return new Response("Permission denied to delete this slug.", { status: 403 });
|
||||
|
||||
const newSlugs = slugs.filter(s => s !== slug);
|
||||
|
||||
await Promise.all([
|
||||
env.KV_EV.delete(slug),
|
||||
env.D1_EV.prepare("UPDATE users SET custom_slugs = ? WHERE username = ?").bind(JSON.stringify(newSlugs), username).run()
|
||||
]);
|
||||
|
||||
return Response.json({ success: true, slug });
|
||||
} catch (e) {
|
||||
return new Response(e.message, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user