From 3375311c1b92f615940412bb89ce81f644753996 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 25 Jul 2024 00:13:29 -0700 Subject: feat(notifications): allow unsubscribe --- .../api/notifications/unsubscribe/+server.ts | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/routes/api/notifications/unsubscribe/+server.ts (limited to 'src/routes/api') diff --git a/src/routes/api/notifications/unsubscribe/+server.ts b/src/routes/api/notifications/unsubscribe/+server.ts new file mode 100644 index 00000000..f122625d --- /dev/null +++ b/src/routes/api/notifications/unsubscribe/+server.ts @@ -0,0 +1,26 @@ +import { userIdentity } from '$lib/Data/AniList/identity'; +import { deleteUserSubscription } from '$lib/Database/userNotifications'; + +const unauthorised = new Response('Unauthorised', { status: 401 }); + +export const POST = async ({ cookies }) => { + 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 deleteUserSubscription(userId); + + return new Response(null, { status: 200 }); +}; -- cgit v1.2.3