58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useState } from "react";
|
||
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
||
|
|
import { Button } from "@/components/ui/button";
|
||
|
|
import { Input } from "@/components/ui/input";
|
||
|
|
import { Label } from "@/components/ui/label";
|
||
|
|
|
||
|
|
export function LoginForm() {
|
||
|
|
const router = useRouter();
|
||
|
|
const searchParams = useSearchParams();
|
||
|
|
const [secret, setSecret] = useState("");
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
const [submitting, setSubmitting] = useState(false);
|
||
|
|
|
||
|
|
async function handleSubmit(e: React.FormEvent) {
|
||
|
|
e.preventDefault();
|
||
|
|
setSubmitting(true);
|
||
|
|
setError(null);
|
||
|
|
|
||
|
|
const res = await fetch("/api/auth/login", {
|
||
|
|
method: "POST",
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
body: JSON.stringify({ secret }),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!res.ok) {
|
||
|
|
setSubmitting(false);
|
||
|
|
setError("Incorrect secret.");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
router.push(searchParams.get("from") ?? "/");
|
||
|
|
router.refresh();
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
|
||
|
|
<div className="flex flex-col gap-2">
|
||
|
|
<Label htmlFor="secret">Access secret</Label>
|
||
|
|
<Input
|
||
|
|
id="secret"
|
||
|
|
type="password"
|
||
|
|
autoFocus
|
||
|
|
autoComplete="current-password"
|
||
|
|
value={secret}
|
||
|
|
onChange={(e) => setSecret(e.target.value)}
|
||
|
|
placeholder="••••••••"
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
{error && <p className="text-sm text-destructive">{error}</p>}
|
||
|
|
<Button type="submit" disabled={submitting || secret.length === 0} className="w-full">
|
||
|
|
{submitting ? "Checking..." : "Enter"}
|
||
|
|
</Button>
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|