Socialify

Folder ..

Viewing exchange-token.js
76 lines (69 loc) • 2.1 KB

 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
71
72
73
74
75
76
export async function onRequest(context) {
  const url = new URL(context.request.url);
  const path = url.pathname;

  if (path === '/exchange-token') {
    return handleTokenExchange(context);
  } else {
    return new Response('Not found', { status: 404 });
  }
}

async function handleTokenExchange(context) {
  const request = context.request;
  if (request.method !== 'POST') {
    return new Response('Method Not Allowed', { status: 405 });
  }

  try {
    const data = await request.json();
    const code = data.code;
    if (!code) {
      return new Response('Authorization code is required', { status: 400 });
    }

    const payload = {
      client_id: context.env.VITE_CLIENT_ID,
      client_secret: context.env.VITE_CLIENT_SECRET,
      code,
      grant_type: 'authorization_code',
      redirect_uri: context.env.VITE_REDIRECT_URI,
    };

    const apiResponse = await fetch('https://anilist.co/api/v2/oauth/token', {
      method: 'POST',
      body: JSON.stringify(payload),
      headers: {
        'Content-Type': 'application/json',
        'Accept-Encoding': 'identity',
      },
    });

    const responseBody = await apiResponse.text();
    if (!apiResponse.ok) {
      console.error('API response error:', responseBody);
      throw new Error(`API responded with status: ${apiResponse.status}`);
    }

    const responseData = JSON.parse(responseBody);
    if (responseData.access_token) {
      return new Response(
        JSON.stringify({ accessToken: responseData.access_token }),
        {
          headers: { 'Content-Type': 'application.json' },
        },
      );
    } else {
      console.error(
        'Access token not found in the API response:',
        responseBody,
      );
      throw new Error('Access token not found in the response');
    }
  } catch (error) {
    console.error(`Error when handling token exchange: ${error}`);
    return new Response(
      JSON.stringify({
        error: 'Failed to exchange token',
        details: error.message,
      }),
      {
        status: 500,
        headers: { 'Content-Type': 'application.json' },
      },
    );
  }
}