haixunMaster/components/auth/secret-register-dialog.tsx

119 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { FormEvent, useState } from "react";
import { useRouter } from "next/navigation";
import { Loader2, UserPlus } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { notify } from "@/lib/notifications/store";
interface SecretRegisterDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
}
export function SecretRegisterDialog({ open, onOpenChange }: SecretRegisterDialogProps) {
const router = useRouter();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [name, setName] = useState("");
const [loading, setLoading] = useState(false);
async function handleSubmit(event: FormEvent) {
event.preventDefault();
setLoading(true);
try {
const res = await fetch("/api/auth/register", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email,
password,
name: name.trim() || undefined,
}),
});
const data = await res.json();
if (!res.ok) {
notify({ type: "error", title: data.error ?? "註冊失敗" });
setLoading(false);
return;
}
notify({ type: "success", title: "新帳號已建立", message: "已切換至新帳號" });
onOpenChange(false);
setEmail("");
setPassword("");
setName("");
if (data.needsThreadsBind) {
router.replace("/matrix?bind_threads=1");
return;
}
router.replace("/matrix");
} catch {
notify({ type: "error", title: "連線失敗,請稍後再試" });
setLoading(false);
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
<DialogTitle></DialogTitle>
<DialogDescription>使</DialogDescription>
</DialogHeader>
<form className="space-y-4" onSubmit={handleSubmit}>
<div className="space-y-2">
<Label htmlFor="secret-register-name"></Label>
<Input
id="secret-register-name"
value={name}
onChange={(event) => setName(event.target.value)}
placeholder="你的名字"
autoComplete="name"
/>
</div>
<div className="space-y-2">
<Label htmlFor="secret-register-email">Email</Label>
<Input
id="secret-register-email"
type="email"
value={email}
onChange={(event) => setEmail(event.target.value)}
placeholder="you@example.com"
autoComplete="email"
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="secret-register-password"></Label>
<Input
id="secret-register-password"
type="password"
value={password}
onChange={(event) => setPassword(event.target.value)}
placeholder="至少 6 個字元"
autoComplete="new-password"
required
/>
</div>
<Button className="w-full" type="submit" disabled={loading}>
{loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <UserPlus className="h-4 w-4" />}
</Button>
</form>
</DialogContent>
</Dialog>
);
}