aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/routes/admin.users.tsx
blob: 6c9b60d7d0db249124bbb7a7fc6aac0cb667f4df (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
45
46
47
48
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { isAuthenticated } from "../.server/auth";
import { apiClient } from "../.server/api/client";

export const meta: MetaFunction = () => {
  return [{ title: "[Admin] Users | iOSDC Japan 2024 Albatross.swift" }];
};

export async function loader({ request }: LoaderFunctionArgs) {
  const { user, token } = await isAuthenticated(request, {
    failureRedirect: "/login",
  });
  if (!user.is_admin) {
    throw new Error("Unauthorized");
  }
  const { data, error } = await apiClient.GET("/admin/users", {
    params: {
      header: {
        Authorization: `Bearer ${token}`,
      },
    },
  });
  if (error) {
    throw new Error(error.message);
  }
  return { users: data.users };
}

export default function AdminUsers() {
  const { users } = useLoaderData<typeof loader>()!;

  return (
    <div>
      <div>
        <h1>[Admin] Users</h1>
        <ul>
          {users.map((user) => (
            <li key={user.user_id}>
              {user.display_name} (id={user.user_id} username={user.username})
              {user.is_admin && <span> admin</span>}
            </li>
          ))}
        </ul>
      </div>
    </div>
  );
}