Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ログインができるように #11

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/components/login.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.loginForm {
display: flex;
flex-direction: column;
align-items: center;

h1 {
text-align: center;
}
}
36 changes: 36 additions & 0 deletions app/lib/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export type LoginArgs = {
name: string;
passphrase: string;
};

export const login = async ({
name,
passphrase,
}: LoginArgs): Promise<
| { error: string }
| {
authorization_token: string;
}
> => {
try {
const response = await fetch("http://localhost:3000/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name, passphrase, captcha_token: "" }),
});

if (!response.ok) {
console.error(response);
if (response.status === 400) {
throw new Error("Invalid credentials");
}
throw new Error("Unknown error");
}
const { authorization_token } = await response.json();
return { authorization_token };
} catch (e) {
return { error: (e as Error).message };
}
};
10 changes: 10 additions & 0 deletions app/root.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.root {
width: 50svw;
max-width: 60rem;
height: 100vh;
margin: 0 auto;
laminne marked this conversation as resolved.
Show resolved Hide resolved
box-sizing: border-box;
padding: 0.5rem;
border: solid;
border-width: 0 0.5px;
}
3 changes: 2 additions & 1 deletion app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Links, Meta, Outlet, Scripts } from "@remix-run/react";
import styles from "~/root.module.css";

export function Layout({ children }: { children: React.ReactNode }) {
return (
Expand All @@ -9,7 +10,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
<Meta />
<Links />
</head>
<body>
<body className={styles.root}>
{children}
<Scripts />
</body>
Expand Down
70 changes: 70 additions & 0 deletions app/routes/login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ActionFunctionArgs } from "@remix-run/node";
import { Form, MetaFunction, useActionData } from "@remix-run/react";
import { useEffect } from "react";
import styles from "~/components/login.module.css";
import { login } from "~/lib/login";

export const meta: MetaFunction = () => {
return [
{
title: "Log in | Caramel",
},
{
content: "noindex",
},
];
};

export const action = async ({
request,
}: ActionFunctionArgs): Promise<
| { authorization_token: string }
| {
error: string;
}
> => {
const formData = await request.formData();
const name = formData.get("name") as string;
const passphrase = formData.get("passphrase") as string;
return login({ name, passphrase });
};

export default function Login() {
const data = useActionData<typeof action>();

useEffect(() => {
if (!data) return;
if ("error" in data) return;

localStorage.setItem("authToken", data.authorization_token);
// FIXME: Should use redirect() ?
window.location.href = "/";
}, [data]);

return (
<>
<h1 className={styles.loginForm}>Welcome back</h1>

{(() => {
if (!data) return null;
if ("error" in data) return <p>{data.error}</p>;
return <p>Logged in</p>;
})()}

<Form method="post" className={styles.loginForm}>
<label htmlFor="name">Account name [required]</label>
<input
type="text"
id="name"
name="name"
required
placeholder="@[email protected]"
/>
<label htmlFor="password">Passphrase [required]</label>
<input type="password" id="password" name="passphrase" required />
MikuroXina marked this conversation as resolved.
Show resolved Hide resolved

<button type="submit">Log in</button>
</Form>
</>
);
}
3 changes: 3 additions & 0 deletions app/styles/login.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}