to any dashboard page.
* Requires elements with IDs: userInitials, userName, userTier
*/
(async function loadUserProfile() {
const token = localStorage.getItem('regpulse_token') || localStorage.getItem('token') || '';
if (!token) return;
try {
const res = await fetch('/api/me', { headers: { 'Authorization': 'Bearer ' + token } });
if (!res.ok) return;
const data = await res.json();
if (!data.success || !data.subscriber) return;
const sub = data.subscriber;
const email = sub.email || '';
const company = sub.company || '';
// Derive display name: use company if available, otherwise email prefix
const displayName = company || email.split('@')[0];
// Derive initials
const parts = displayName.replace(/[^a-zA-Z0-9 ]/g, '').trim().split(/\s+/);
const initials = parts.length >= 2
? (parts[0][0] + parts[parts.length - 1][0]).toUpperCase()
: displayName.slice(0, 2).toUpperCase();
const tierLabel = (sub.tier || 'free').charAt(0).toUpperCase() + (sub.tier || 'free').slice(1);
const el = (id) => document.getElementById(id);
if (el('userInitials')) el('userInitials').textContent = initials;
if (el('userName')) el('userName').textContent = displayName;
if (el('userTier')) el('userTier').textContent = tierLabel;
} catch (e) { /* silent */ }
})();