33 lines
1.4 KiB
TypeScript
33 lines
1.4 KiB
TypeScript
"use client";
|
|
|
|
import { useFormState } from "react-dom";
|
|
import { signIn, signUp, type ActionResult } from "./actions";
|
|
|
|
const initialState: ActionResult = {};
|
|
|
|
export function AuthForm() {
|
|
const [signUpState, signUpAction] = useFormState(signUp, initialState);
|
|
const [signInState, signInAction] = useFormState(signIn, initialState);
|
|
|
|
return (
|
|
<div style={{ display: "grid", gap: 24 }}>
|
|
<form action={signUpAction} style={{ display: "grid", gap: 8 }}>
|
|
<h2 style={{ margin: 0 }}>Create an account</h2>
|
|
<input name="email" type="email" placeholder="you@example.com" required />
|
|
<input name="password" type="password" placeholder="password (6+ chars)" required minLength={6} />
|
|
<button type="submit">Sign up</button>
|
|
{signUpState.error && <p style={{ color: "crimson" }}>{signUpState.error}</p>}
|
|
{signUpState.message && <p>{signUpState.message}</p>}
|
|
</form>
|
|
|
|
<form action={signInAction} style={{ display: "grid", gap: 8 }}>
|
|
<h2 style={{ margin: 0 }}>Already have an account</h2>
|
|
<input name="email" type="email" placeholder="you@example.com" required />
|
|
<input name="password" type="password" placeholder="password" required />
|
|
<button type="submit">Sign in</button>
|
|
{signInState.error && <p style={{ color: "crimson" }}>{signInState.error}</p>}
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|