-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
55 lines (46 loc) · 1.43 KB
/
actions.ts
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
/* eslint-disable */
"use server";
import { deleteTask, getTask } from "@/data-access/tasks";
import { getSession } from "@/app/api/auth/[...nextauth]/options";
import { revalidatePath } from "next/cache";
import { useSession } from "next-auth/react";
/**
*BrowseActions
* @description Serverseitige Aktionen für Aufgabenverwaltung.
* @remarks
* Enthält Funktionen zum Auflisten, Filtern und Sortieren von Aufgaben.
* @link Verwandt mit {@link BrowsePage}
*/
export async function deleteAsAdminTask(taskId: string) {
const session = await getSession();
// const session = useSession();
console.log("Session:", session);
console.log("User Role:", session?.user.role);
if (!session) {
throw new Error("User not authenticated");
}
// .data?.user?.role
const isAdmin = session.user.role === "admin";
const task = await getTask(taskId);
console.log("User ID:", session.user.id);
console.log("Task User ID:", task?.userId);
console.log("Is Admin:", isAdmin);
if (!session?.user.role === isAdmin) {
throw new Error("User not authorized");
}
await deleteTask(taskId);
revalidatePath("/browse");
}
export async function getServerSideProps() {
const session = await getSession();
if (!session) {
return {
redirect: {
destination: "/api/auth/signin?callbackUrl=/protected-page",
permanent: false,
},
};
}
// Additional checks for roles or permissions can also be performed here
return { props: { session } };
}