import { action, redirect, useSubmission } from "@solidjs/router";
import { type Accessor, Show } from "solid-js";
export default function Home() {
const login = action(async (data: FormData) => {
"use server";
const username = data.get("username");
const password = data.get("password");
if (typeof username !== "string" || typeof password !== "string")
throw new Error("Invalid input");
if (username === "a" && password === "b") throw redirect("/register");
if (encodeURIComponent(username) !== username)
throw new Error("Invalid username");
return JSON.stringify({ username, password });
}, "login");
const form = useSubmission(login);
return (
<div>
<h1>Login</h1>
<Show when={form.pending}>
<p>Submitting...</p>
</Show>
<Show when={form.result}>{(result) => <p>{result()}</p>}</Show>
<p>
Don't have an account? <a href="/register">Register</a>
</p>
<Show when={form.error}>
{(error: Accessor<Error>) => <span>{error().message}</span>}
</Show>
<form action={login} method="post">
<div>
<input name="username" required placeholder="Username" />
</div>
<div>
<input
required
type="password"
name="password"
placeholder="Password"
/>
</div>
<button>Submit</button>
</form>
</div>
);
}