aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/configuration/+server.ts
blob: 41a70f7bc6cdbf613c4dde208463e6639d5346c4 (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
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
66
67
68
69
70
import { userIdentity } from '$lib/Data/AniList/identity';
import {
  deleteUserConfiguration,
  getUserConfiguration,
  setUserConfiguration
} from '$lib/Database/SB/User/configuration';

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

export const GET = async ({ url }) =>
  Response.json(await getUserConfiguration(Number(url.searchParams.get('id') || 0)), {
    headers: {
      'Access-Control-Allow-Origin': 'https://due.moe'
    }
  });

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

  if (!userCookie) return unauthorised;

  const user = JSON.parse(userCookie);

  return Response.json(
    await setUserConfiguration(
      (
        await userIdentity({
          tokenType: user['token_type'],
          expiresIn: user['expires_in'],
          accessToken: user['access_token'],
          refreshToken: user['refresh_token']
        })
      ).id,
      {
        configuration: await request.json()
      }
    ),
    {
      headers: {
        'Access-Control-Allow-Origin': 'https://due.moe'
      }
    }
  );
};

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

  if (!userCookie) return unauthorised;

  const user = JSON.parse(userCookie);

  return Response.json(
    await deleteUserConfiguration(
      (
        await userIdentity({
          tokenType: user['token_type'],
          expiresIn: user['expires_in'],
          accessToken: user['access_token'],
          refreshToken: user['refresh_token']
        })
      ).id
    ),
    {
      headers: {
        'Access-Control-Allow-Origin': 'https://due.moe'
      }
    }
  );
};