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
|
import { Schema } from "effect";
import { safeUserIdentity } from "$lib/Data/AniList/identity";
import {
deleteUserConfiguration,
getUserConfiguration,
setUserConfiguration,
} from "$lib/Database/SB/User/configuration";
import { decodeAuthCookieOrNull } from "$lib/Effect/authCookie";
import { decodeRequestJsonOrThrow } from "$lib/Effect/requestBody";
import { appOriginHeaders } from "$lib/Utility/appOrigin";
const unauthorised = new Response("Unauthorised", { status: 401 });
const authenticatedUserId = async (cookies: {
get: (name: string) => string | undefined;
}) => {
const userCookie = cookies.get("user");
if (!userCookie) return null;
const user = decodeAuthCookieOrNull(userCookie);
if (!user) return null;
return (await safeUserIdentity(user))?.id ?? null;
};
export const GET = async ({ cookies, url }) => {
const userId = await authenticatedUserId(cookies);
const requestedUserId = Number(url.searchParams.get("id") || 0);
if (!userId || requestedUserId !== userId) return unauthorised;
return Response.json(await getUserConfiguration(requestedUserId), {
headers: appOriginHeaders(),
});
};
export const PUT = async ({ cookies, request }) => {
const userId = await authenticatedUserId(cookies);
if (!userId) return unauthorised;
return Response.json(
await setUserConfiguration(userId, {
configuration: await decodeRequestJsonOrThrow(
request,
Schema.Record(Schema.String, Schema.Unknown),
),
}),
{
headers: appOriginHeaders(),
},
);
};
export const DELETE = async ({ cookies }) => {
const userId = await authenticatedUserId(cookies);
if (!userId) return unauthorised;
return Response.json(await deleteUserConfiguration(userId), {
headers: appOriginHeaders(),
});
};
|