aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/api/v1/whoami/+server.ts
blob: 2b7d43034b1bcdd2c7acfb3426af798db73fb991 (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
import { error, json } from '@sveltejs/kit';
import { filterSession, type Session } from '../../../../hooks.server.js';

export const GET = async ({ locals }) => {
  const data = (await locals.auth()) as Session;

  // note: these return types are JUST for this endpoint - rely on status code exclusively for the actual meaning.
  const headers = {
    'Access-Control-Allow-Origin': '*',
  };
  if (data === undefined)
    return json(
      {
        '': '',
        kind: 'NOT_AUTHENTICATED' as const,
        message: 'Unauthenticated',
      },
      {
        status: 403,
        statusText: 'Forbidden',
        headers,
      }
    );
  if (data === null)
    return json(
      {
        '': '',
        kind: 'EXPIRED' as const,
        message: 'Session Expired',
      },
      {
        status: 401,
        statusText: 'Unauthorized',
        headers,
      }
    );

  return json(filterSession(data), {
    headers,
    status: 200,
    statusText: 'OK :3',
  });
};