aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/notifications/subscribe/+server.ts
blob: d7b4b6a42ed72e905da8f34b252b8e191b7fbb4e (plain) (blame)
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
import { userIdentity } from '$lib/Data/AniList/identity';
import { setUserSubscription } from '$lib/Database/Supabase/userNotifications';

const unauthorised = new Response('Unauthorised', { status: 401 });

export const POST = async ({ cookies, request }) => {
	const userCookie = cookies.get('user');

	if (!userCookie) return unauthorised;

	const user = JSON.parse(userCookie);
	const userId = (
		await userIdentity({
			tokenType: user['token_type'],
			expiresIn: user['expires_in'],
			accessToken: user['access_token'],
			refreshToken: user['refresh_token']
		})
	).id;

	if (!userId) return unauthorised;

	await setUserSubscription(userId, await request.json());

	return new Response(null, { status: 200 });
};