summaryrefslogtreecommitdiff
path: root/api/auth/user.ts
blob: ce6ea93a230362167aabdd376a30c58954609db9 (plain)
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
import type { VercelRequest, VercelResponse } from '@vercel/node';

import getUser from 'db/_getUser';

/**
 * # user
 * 
 * ## GET
 * 
 * Gets the user with the given user ID.
 * 
 * ### Parameters
 * 
 * - `userId: string`. The ID of the user to get.
 * 
 * ### Response
 * 
 * `{ id: string, displayName: string }`. The user with the provided ID.
 */
export default async function handler(request: VercelRequest, response: VercelResponse) {
  switch (request.method) {
    case 'GET': {
      const { userId } = request.body;

      if (!userId) {
        response
          .status(400)
          .send('Expected request body to contain a userId, but none was provided.');
        return;
      }

      try {
        const user = await getUser(userId);
        response.status(200).send(user);
        return;
      } catch (err) {
        response
          .status(400)
          .send(`Failed to get user with ID ${userId}. ${err}`);
        return;
      }
    }
  }
}