add auth to form actions

This commit is contained in:
Akis 2023-01-05 22:48:20 +02:00
parent a95b761ce4
commit 061961e26f
Signed by untrusted user: akis
GPG Key ID: 267BF5C6677944ED

View File

@ -4,7 +4,10 @@ import { fail } from "@sveltejs/kit";
import db from "$lib/db"; import db from "$lib/db";
export const actions: Actions = { export const actions: Actions = {
add: async ({ request }) => { add: async ({ request, locals }) => {
if (!await locals.getSession()) {
return fail(401, { addError: true, addMessage: "You must be logged in to post an announcement." });
} else {
const Announcements = db.model("Announcements"); const Announcements = db.model("Announcements");
const formData = await request.formData(); const formData = await request.formData();
@ -33,8 +36,13 @@ export const actions: Actions = {
return { addSuccess: true, addMessage: "Your announcement has been posted." }; return { addSuccess: true, addMessage: "Your announcement has been posted." };
} }
}
}, },
delete: async () => { delete: async ({ locals }) => {
if (!await locals.getSession()) {
return fail(401, { deleteError: true, deleteMessage: "You must be logged in to delete an announcement." });
} else {
const Announcements = db.model("Announcements"); const Announcements = db.model("Announcements");
await Announcements.sync(); await Announcements.sync();
@ -43,4 +51,5 @@ export const actions: Actions = {
return { deleteSuccess: true, deleteMessage: "Your announcement has been deleted." }; return { deleteSuccess: true, deleteMessage: "Your announcement has been deleted." };
} }
}
} }