"use client"; import Image from "next/image"; import { useState } from "react"; import { LinkIcon } from "lucide-react"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { authClient } from "@/lib/auth/client"; export function GoogleAccountSettings({ googleAccountIds: initialGoogleAccountIds, hasOtherAuthMethod, linkError, }: { googleAccountIds: string[]; hasOtherAuthMethod: boolean; linkError: boolean; }) { const [googleAccountIds, setGoogleAccountIds] = useState(initialGoogleAccountIds); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const linked = googleAccountIds.length > 0; const canUnlink = hasOtherAuthMethod || googleAccountIds.length > 1; async function linkGoogle() { if (busy) return; setBusy(true); setError(""); try { const result = await authClient.linkSocial({ provider: "google", requestSignUp: false, callbackURL: "/profile", errorCallbackURL: "/profile?googleLinkError=1", }); if (result.error) throw new Error(result.error.message ?? "เชื่อมบัญชี Google ไม่สำเร็จ"); } catch (cause) { setError(cause instanceof Error ? cause.message : "เชื่อมบัญชี Google ไม่สำเร็จ กรุณาลองอีกครั้ง"); } finally { setBusy(false); } } async function unlinkGoogle() { const accountId = googleAccountIds[0]; if (busy || !accountId || !canUnlink) return; setBusy(true); setError(""); try { const result = await authClient.unlinkAccount({ accountId }); if (result.error) throw new Error(result.error.message ?? "ยกเลิกการเชื่อมต่อ Google ไม่สำเร็จ"); setGoogleAccountIds((current) => current.filter((id) => id !== accountId)); } catch (cause) { setError(cause instanceof Error ? cause.message : "ยกเลิกการเชื่อมต่อ Google ไม่สำเร็จ กรุณาลองอีกครั้ง"); } finally { setBusy(false); } } return ( เชื่อมต่อบัญชี Google เพื่อเข้าสู่ระบบได้ง่ายขึ้น

Google

{linked ? "เชื่อมต่อแล้ว" : "ยังไม่ได้เชื่อมต่อ"}

{linked ? : }
{linked && !canUnlink &&

Google เป็นวิธีเข้าสู่ระบบเดียวของบัญชีนี้ เพิ่มวิธีเข้าสู่ระบบอื่นก่อนยกเลิกการเชื่อมต่อ

} {(linkError || error) && {error || "เชื่อมบัญชี Google ไม่สำเร็จ กรุณาลองอีกครั้ง"} }
); }