'use client' // Settings → Profile: editable particulars (display name, bio, location, // occupation, organization, website). Values load from GET /api/me — the // Go identity is re-read per request, so this is always the stored truth, // not the login-time NextAuth snapshot. After a save, useSession().update() // re-syncs the session identity (auth.ts jwt "update" trigger) so the top // bar and this header reflect a new name without a re-login. import { useEffect, useState } from 'react' import { useSession } from 'next-auth/react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { User } from 'lucide-react' import { toast } from 'sonner' type Profile = { name: string bio: string location: string occupation: string organization: string website: string } const EMPTY: Profile = { name: '', bio: '', location: '', occupation: '', organization: '', website: '' } function fromUser(u: Record | null | undefined): Profile { const s = (v: unknown) => (typeof v === 'string' ? v : '') return { name: s(u?.name), bio: s(u?.bio), location: s(u?.location), occupation: s(u?.occupation), organization: s(u?.organization), website: s(u?.website), } } export function ProfileSettings() { const { data: session, update } = useSession() const [profile, setProfile] = useState(EMPTY) const [baseline, setBaseline] = useState(EMPTY) const [loaded, setLoaded] = useState(false) const [saving, setSaving] = useState(false) useEffect(() => { fetch('/api/me') .then(r => r.json()) .then(data => { const p = fromUser(data?.user) setProfile(p) setBaseline(p) }) .catch(() => { /* the form stays usable; saving surfaces any outage */ }) .finally(() => setLoaded(true)) }, []) const dirty = loaded && JSON.stringify(profile) !== JSON.stringify(baseline) const set = (key: keyof Profile) => (e: React.ChangeEvent) => setProfile(p => ({ ...p, [key]: e.target.value })) const save = async () => { setSaving(true) try { const res = await fetch('/api/me', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(profile), }) const data = await res.json().catch(() => null) if (res.ok && data) { const p = fromUser(data) setProfile(p) setBaseline(p) toast.success('Profile saved') // Refresh the NextAuth identity so the top bar picks up the name. // The argument matters: in next-auth v5 a bare update() only // re-fetches the session; update({}) POSTs and fires the jwt // callback with trigger="update" (the auth.ts re-sync path). await update({}) } else { toast.error(data?.error ?? 'Failed to save') } } catch { toast.error('Failed to save') } finally { setSaving(false) } } const displayName = profile.name || session?.user?.name || session?.user?.email || 'User' return (

Profile

{displayName[0]?.toUpperCase() ?? '?'}

{displayName}

{session?.user?.email}

Your name appears when you share bookmarks, tags and collections with others. Public bookmark links never reveal your identity.