Use saved code

This commit is contained in:
2026-10-29 16:26:36 -03:00
parent 08f10022e0
commit 7608408e59
3 changed files with 64 additions and 23 deletions
+30 -1
View File
@@ -1,16 +1,45 @@
import { Signal, useSignal, useSignalEffect } from "@preact/signals";
import Form from "@/islands/Form.tsx";
import { Signal } from "@preact/signals";
import { post_json } from "@/src/client.ts";
interface AskCodeProps {
page: Signal<string>;
}
export default function AskCode({ page }: AskCodeProps) {
const checking = useSignal(false);
const on_success = (data: { code: string }) => {
localStorage.setItem("code", data.code);
page.value = "keys";
};
useSignalEffect(() => {
const fn = async () => {
const code = localStorage.getItem("code");
if (!code) {
return;
}
checking.value = true;
const result = await post_json<{ code: string }>("/api/verify_code", { code });
checking.value = false;
if (result.ok) {
on_success(result.data);
} else if (result.status !== 0) {
// Network errors keep the code so it can be retried on the next load
localStorage.removeItem("code");
}
};
fn();
});
if (checking.value) {
return <p>Verifying code...</p>;
}
return (
<Form action="/api/verify_code" on_success={on_success}>
<input type="text" name="code" />