aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/routes/Login.tsx
blob: f3fa00ef3fbfbe81f421f72087ff95784e1fe85c (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
import { redirect, Form, ActionFunctionArgs } from "react-router-dom";

export default function Login() {
  return (
    <div>
      <h1>Albatross.swift</h1>
      <h2>
        Login
      </h2>
      <Form method="post">
        <label>Username</label>
        <input type="text" name="username" />
        <label>Password</label>
        <input type="password" name="password" />
        <button type="submit">Login</button>
      </Form>
    </div>
  );
};

export async function loginAction({ request }: ActionFunctionArgs) {
  const formData = await request.formData();
  const username = formData.get("username");
  const password = formData.get("password");

  const res = await fetch("/api/login", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ username, password }),
  });
  if (!res.ok) {
    throw res;
  }
  const { userId } = await res.json();
  return redirect(`/users/${userId}/`);
};