Files
wikideck/src/auth/LoginButton.tsx
T
claudeandClaude Opus 5.5 feae445f5a Night 1: eom.dev accounts, Dex OIDC login (PKCE) and wallet linking (#15)
OIDC PKCE login, ID-token verification against Dex JWKS, a client-built wallet link message, an off-chain account and wallet store, and a mobile-safe login button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 04:12:50 +00:00

77 lines
4.2 KiB
TypeScript

import { useEffect, useState, useSyncExternalStore } from 'react';
import { Link2, LoaderCircle, LogIn, LogOut, UserRound } from 'lucide-react';
import { getWallet, subscribeWallet, type Wallet } from '../wallet.ts';
import { account, completeLogin, isCallback, linkWallet, login, logout, subscribeAuth, type Account } from './session.ts';
import './auth.css';
export function useAccount(): Account | undefined {
return useSyncExternalStore(subscribeAuth, account, () => undefined);
}
/** Deterministic initials avatar (LDAP accounts have no picture claim; no third-party image hosts). */
export function Avatar({ name, seed, size = 22 }: { name: string; seed: string; size?: number }) {
let h = 0; for (const c of seed) h = (h * 31 + c.charCodeAt(0)) >>> 0;
const initials = name.split(/\s+/).filter(Boolean).slice(0, 2).map(w => [...w][0]).join('').toUpperCase() || '?';
return <span className="auth-avatar" aria-hidden="true"
style={{ width: size, height: size, fontSize: size * 0.42, background: `hsl(${h % 360} 45% 42%)` }}>{initials}</span>;
}
const short = (s: string) => `${s.slice(0, 6)}…${s.slice(-4)}`;
export function LoginButton() {
const me = useAccount();
const [busy, setBusy] = useState(false);
const [open, setOpen] = useState(false);
const [notice, setNotice] = useState('');
const [wallet, setWallet] = useState<Wallet>();
useEffect(() => {
if (!isCallback(location.href)) return;
setBusy(true);
completeLogin(location.href)
.then(({ returnTo }) => history.replaceState(null, '', returnTo))
.catch(e => { history.replaceState(null, '', '/'); setNotice(e instanceof Error ? e.message : 'Login failed'); })
.finally(() => setBusy(false));
}, []);
useEffect(() => {
const update = () => { void getWallet().then(setWallet).catch(() => setWallet(undefined)); };
update(); return subscribeWallet(update);
}, []);
const run = async (fn: () => Promise<unknown>) => {
setBusy(true); setNotice('');
try { await fn(); } catch (e) { setNotice(e instanceof Error ? e.message : 'Request failed'); }
finally { setBusy(false); }
};
if (!me) return <span className="auth-wrap">
<button className="wallet-button auth-button" disabled={busy} aria-label="Log in with eom.dev" title="Log in with eom.dev" onClick={() => run(() => login())}>
<span className="wallet-avatar">{busy ? <LoaderCircle className="spin" size={14} /> : <LogIn size={14} />}</span>
<span className="auth-label">Log in with eom.dev</span>
</button>
{notice && <span className="auth-notice" role="alert">{notice}</span>}
</span>;
const linked = wallet && me.wallets.some(w => w.address === wallet.address);
return <span className="auth-wrap">
<button className="wallet-button auth-button" aria-expanded={open} aria-label={`eom.dev account: ${me.name}`} onClick={() => setOpen(o => !o)}>
<Avatar name={me.name} seed={me.sub} /><span className="auth-label">{me.name}</span>
</button>
{open && <div className="auth-menu" role="menu">
<div className="auth-menu-head"><Avatar name={me.name} seed={me.sub} size={34} />
<div><strong>{me.name}</strong>{me.username && <small>@{me.username} · eom.dev</small>}</div></div>
<div className="auth-menu-section">
<small>LINKED WALLETS</small>
{me.wallets.length ? me.wallets.map(w => <code key={w.address}>{short(w.address)}</code>) : <p>None yet.</p>}
</div>
{linked ? <p className="auth-ok"><UserRound size={14} /> This wallet is linked.</p>
: <>{wallet && <p className="auth-consent">Linking shows your eom.dev name ({me.name}) next to this wallet address on leaderboards, and anyone can look it up. You can unlink later.</p>}<button className="button dark full" role="menuitem" disabled={busy || !wallet} onClick={() => run(async () => {
await linkWallet(wallet); setNotice('Wallet linked.');
})}>{busy ? <LoaderCircle className="spin" size={15} /> : <Link2 size={15} />}
{wallet ? `Link wallet ${short(wallet.address)}` : 'Unlock your wallet to link it'}</button></>}
{notice && <p className="auth-notice" role="status">{notice}</p>}
<button className="button full" role="menuitem" onClick={() => { logout(); setOpen(false); }}><LogOut size={15} /> Log out</button>
</div>}
</span>;
}