|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { useTranslations } from "next-intl"; |
| 5 | + |
| 6 | +type LinkedIdentity = { |
| 7 | + provider: string; |
| 8 | + displayNameAtLink: string | null; |
| 9 | + linkedAt: string | null; |
| 10 | + lastLoginAt: string | null; |
| 11 | +}; |
| 12 | + |
| 13 | +function getToken(): string | null { |
| 14 | + if (typeof window === "undefined") return null; |
| 15 | + return localStorage.getItem("satoken"); |
| 16 | +} |
| 17 | + |
| 18 | +const PROVIDER_LABEL: Record<string, string> = { |
| 19 | + github: "GitHub", |
| 20 | + discord: "Discord", |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * 已绑定登录方式的查看 + 解绑(后端 M2a)。绑定新 provider 走 OAuth 流程, |
| 25 | + * 待绑定流程(M2b)上线后再加"连接"按钮——现在加会把用户登成新账号(分叉)。 |
| 26 | + */ |
| 27 | +export function LinkedAccounts() { |
| 28 | + const t = useTranslations("settings.linked"); |
| 29 | + const [items, setItems] = useState<LinkedIdentity[] | null>(null); |
| 30 | + const [error, setError] = useState<string | null>(null); |
| 31 | + const [busy, setBusy] = useState<string | null>(null); |
| 32 | + |
| 33 | + async function load() { |
| 34 | + const token = getToken(); |
| 35 | + if (!token) return; |
| 36 | + try { |
| 37 | + const res = await fetch("/api/user-center/identities", { |
| 38 | + headers: { satoken: token }, |
| 39 | + }); |
| 40 | + if (!res.ok) throw new Error(); |
| 41 | + const body = await res.json(); |
| 42 | + setItems(body.data ?? []); |
| 43 | + setError(null); |
| 44 | + } catch { |
| 45 | + setError(t("loadFail")); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + load(); |
| 51 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 52 | + }, []); |
| 53 | + |
| 54 | + async function unbind(provider: string) { |
| 55 | + const token = getToken(); |
| 56 | + if (!token) return; |
| 57 | + setBusy(provider); |
| 58 | + try { |
| 59 | + const res = await fetch(`/api/user-center/identities/${provider}`, { |
| 60 | + method: "DELETE", |
| 61 | + headers: { satoken: token }, |
| 62 | + }); |
| 63 | + const body = await res.json(); |
| 64 | + if (!res.ok || body.success === false) { |
| 65 | + // 后端把"最后一种登录方式不能解绑"等作为 400 + message 返回 |
| 66 | + setError(body.message ?? t("unbindFail")); |
| 67 | + return; |
| 68 | + } |
| 69 | + setItems(body.data ?? []); |
| 70 | + setError(null); |
| 71 | + } catch { |
| 72 | + setError(t("unbindFail")); |
| 73 | + } finally { |
| 74 | + setBusy(null); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (items === null && !error) return null; // 未登录或加载中,保持安静 |
| 79 | + |
| 80 | + return ( |
| 81 | + <section className="mt-10"> |
| 82 | + <label className="block font-serif font-bold text-lg mb-3"> |
| 83 | + {t("label")} |
| 84 | + </label> |
| 85 | + {error && ( |
| 86 | + <p className="font-mono text-xs text-[#CC0000] mb-3">{error}</p> |
| 87 | + )} |
| 88 | + <ul className="border border-[var(--foreground)] divide-y divide-[var(--foreground)]/20"> |
| 89 | + {(items ?? []).map((it) => ( |
| 90 | + <li |
| 91 | + key={it.provider} |
| 92 | + className="flex items-center justify-between px-4 py-3" |
| 93 | + > |
| 94 | + <span className="font-mono text-sm text-[var(--foreground)]"> |
| 95 | + {PROVIDER_LABEL[it.provider] ?? it.provider} |
| 96 | + {it.displayNameAtLink ? ` · ${it.displayNameAtLink}` : ""} |
| 97 | + </span> |
| 98 | + <button |
| 99 | + type="button" |
| 100 | + onClick={() => unbind(it.provider)} |
| 101 | + disabled={busy === it.provider || (items ?? []).length <= 1} |
| 102 | + title={(items ?? []).length <= 1 ? t("lastHint") : undefined} |
| 103 | + className="font-mono text-xs uppercase tracking-widest text-[var(--foreground)]/70 hover:text-[#CC0000] transition-colors disabled:opacity-40 disabled:cursor-not-allowed" |
| 104 | + > |
| 105 | + {busy === it.provider ? "…" : t("unbind")} |
| 106 | + </button> |
| 107 | + </li> |
| 108 | + ))} |
| 109 | + {(items ?? []).length === 0 && ( |
| 110 | + <li className="px-4 py-3 font-mono text-sm text-neutral-500"> |
| 111 | + {t("empty")} |
| 112 | + </li> |
| 113 | + )} |
| 114 | + </ul> |
| 115 | + </section> |
| 116 | + ); |
| 117 | +} |
0 commit comments