'use client' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { authFetch, clearToken, getApiBase, getToken } from '../lib/auth' type ProfileInfo = { username: string role: string auth_provider: string } type ContactInfo = { email?: string | null discord?: string | null telegram?: string | null matrix?: string | null } export default function ProfilePage() { const router = useRouter() const [profile, setProfile] = useState(null) const [contact, setContact] = useState({}) const [currentPassword, setCurrentPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [status, setStatus] = useState(null) const [contactStatus, setContactStatus] = useState(null) const [referrals, setReferrals] = useState< { code: string; uses_count?: number; max_uses?: number | null }[] >([]) const [referralStatus, setReferralStatus] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { if (!getToken()) { router.push('/login') return } const load = async () => { try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/me`) if (!response.ok) { clearToken() router.push('/login') return } const data = await response.json() setProfile({ username: data?.username ?? 'Unknown', role: data?.role ?? 'user', auth_provider: data?.auth_provider ?? 'local', }) const contactResponse = await authFetch(`${baseUrl}/auth/contact`) if (contactResponse.ok) { const contactData = await contactResponse.json() setContact({ email: contactData?.contact?.email ?? '', discord: contactData?.contact?.discord ?? '', telegram: contactData?.contact?.telegram ?? '', matrix: contactData?.contact?.matrix ?? '', }) } const referralResponse = await authFetch(`${baseUrl}/auth/referrals`) if (referralResponse.ok) { const referralData = await referralResponse.json() if (Array.isArray(referralData?.invites)) { setReferrals(referralData.invites) } } } catch (err) { console.error(err) setStatus('Could not load your profile.') } finally { setLoading(false) } } void load() }, [router]) const submit = async (event: React.FormEvent) => { event.preventDefault() setStatus(null) if (!currentPassword || !newPassword) { setStatus('Enter your current password and a new password.') return } try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/password`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ current_password: currentPassword, new_password: newPassword, }), }) if (!response.ok) { const text = await response.text() throw new Error(text || 'Update failed') } setCurrentPassword('') setNewPassword('') setStatus('Password updated.') } catch (err) { console.error(err) setStatus('Could not update password. Check your current password.') } } const submitContact = async (event: React.FormEvent) => { event.preventDefault() setContactStatus(null) try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/contact`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: contact.email, discord: contact.discord, telegram: contact.telegram, matrix: contact.matrix, }), }) if (!response.ok) { const text = await response.text() throw new Error(text || 'Update failed') } setContactStatus('Contact details saved.') } catch (err) { console.error(err) setContactStatus('Could not update contact details.') } } const createReferral = async () => { setReferralStatus(null) try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/referrals`, { method: 'POST' }) if (!response.ok) { const text = await response.text() throw new Error(text || 'Could not create referral invite') } const data = await response.json() if (data?.code) { setReferrals((current) => [ { code: data.code, uses_count: 0, max_uses: 1 }, ...current, ]) } setReferralStatus('Referral invite created.') } catch (err) { console.error(err) setReferralStatus('Could not create a referral invite.') } } if (loading) { return
Loading profile...
} return (

My profile

{profile && (
Signed in as {profile.username} ({profile.role}). Login type:{' '} {profile.auth_provider}.
)} {profile?.auth_provider !== 'local' ? (
Password changes are only available for local Magent accounts.
) : (
{status &&
{status}
}
)}

Contact details

{contactStatus &&
{contactStatus}
}

Referral invites

Share a referral invite with friends or family. Each invite has limited uses.

{referralStatus &&
{referralStatus}
}
{referrals.length === 0 ? (
No referral invites yet.
) : (
Code Uses
{referrals.map((invite) => (
{invite.code} {invite.uses_count ?? 0}/{invite.max_uses ?? '∞'}
))}
)}
) }