'use client' import { verifySignature } from '@/lib/util' import { useGlobal } from '@/providers/GlobalProvider' import { message } from '@tauri-apps/plugin-dialog' import { fetch } from '@tauri-apps/plugin-http' import { openUrl } from '@tauri-apps/plugin-opener' import { Dispatch, SetStateAction, useState } from 'react' function AccountLoggedOut () { const { account } = useGlobal() const [username, setUsername] = useState('') const [password, setPassword] = useState('') return ( <>

Account Login

{ e.preventDefault() const response = await fetch( 'https://games.lncvrt.xyz/api/account/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) } ) const signature = response.headers.get('x-signature') ?? '' const data = await response.json() if (!(await verifySignature(JSON.stringify(data), signature))) { await message( "Failed to login, there was an issue validating the data wasn't tampered with.", { title: 'Request tampered', kind: 'error' } ) return } if (data.success) { if (data.data.session) await account?.set('session', data.data.session) if (data.data.username) await account?.set('name', data.data.username) if (data.data.id) await account?.set('id', data.data.id) if (data.data.admin) await account?.set('admin', data.data.admin) } else { await message(data.message || 'n/a', { title: 'Failed to login', kind: 'error' }) } }} > setUsername(e.target.value)} required className='input-field my-1' /> setPassword(e.target.value)} required className='input-field my-1' />
) } function AccountChangeUsername ({ setChangingUsername }: { setChangingUsername: Dispatch> }) { const { account, accountSession } = useGlobal() const [newUsername, setNewUsername] = useState('') return ( <>

Change Account Username

{ e.preventDefault() const response = await fetch( 'https://games.lncvrt.xyz/api/account/change-username', { method: 'POST', headers: { 'Content-Type': 'application/json', authorization: accountSession ?? '' }, body: JSON.stringify({ newUsername }) } ) const signature = response.headers.get('x-signature') ?? '' const data = await response.json() if (!(await verifySignature(JSON.stringify(data), signature))) { await message( "Failed to change username, there was an issue validating the data wasn't tampered with.", { title: 'Request tampered', kind: 'error' } ) return } if (data.success) { if (data.data) await account?.set('session', data.data) await account?.set('name', newUsername) setChangingUsername(false) } else { await message(data.message || 'n/a', { title: 'Failed to change username', kind: 'error' }) } }} > setNewUsername(e.target.value)} required className='input-field my-1' />
) } function AccountChangePassword ({ setChangingPassword }: { setChangingPassword: Dispatch> }) { const { account, accountSession } = useGlobal() const [newPassword, setNewPassword] = useState('') const [retypeNewPassword, setRetypeNewPassword] = useState('') return ( <>

Change Account Password

{ e.preventDefault() if (newPassword !== retypeNewPassword) { alert('Passwords must match') return } const response = await fetch( 'https://games.lncvrt.xyz/api/account/change-password', { method: 'POST', headers: { 'Content-Type': 'application/json', authorization: accountSession ?? '' }, body: JSON.stringify({ newPassword }) } ) const signature = response.headers.get('x-signature') ?? '' const data = await response.json() if (!(await verifySignature(JSON.stringify(data), signature))) { await message( "Failed to change password, there was an issue validating the data wasn't tampered with.", { title: 'Request tampered', kind: 'error' } ) return } if (data.success) { if (data.data) await account?.set('session', data.data) setChangingPassword(false) } else { await message(data.message || 'n/a', { title: 'Failed to change password', kind: 'error' }) } }} > setNewPassword(e.target.value)} required className='input-field my-1' /> setRetypeNewPassword(e.target.value)} required className='input-field my-1' />
) } function AccountLoggedIn () { const { account, accountName } = useGlobal() const [changingUsername, setChangingUsername] = useState(false) const [changingPassword, setChangingPassword] = useState(false) if (changingUsername) return if (changingPassword) return return ( <>

Account

Logged in as: {accountName}

) } export default function AccountsPage () { const { accountSession, accountId, accountName } = useGlobal() if (!accountSession || !accountId || !accountName) return else return }