const translations = { de: { secureRelay: 'Sicheres Relay', privateByDesign: 'PRIVAT DURCH DESIGN', title: 'Dein Relay. Deine Verbindung.', intro: 'Verwalte den Zugang zu deinem Meets Relay. Aufnahmen und Transkripte werden hier nicht gespeichert.', signOut: 'Abmelden', signIn: 'Anmelden', invitation: 'Einladung', firstSetup: 'Ersteinrichtung', setupCode: 'Einrichtungscode', inviteCode: 'Einladungscode', username: 'Benutzername', password: 'Passwort', macSetup: 'MAC-EINRICHTUNG', authorizeMac: 'Mac mit Relay verbinden?', authorizeDescription: 'Nach deiner Bestätigung erhält die Meets-App auf diesem Mac eine Relay-Berechtigung. Das Browserfenster gibt keine Zugangstoken weiter.', authorize: 'Mac autorisieren', returnMac: 'Zur Meets-App wechseln', adminConsole: 'RELAY-KONTO', pendingMac: 'Mac wartet auf Freigabe', accounts: 'Konten', accountsHint: 'Mitglieder mit Relay-Zugang', invitations: 'Einladungen', invitationsHint: 'Einmal-Codes laufen nach 24 Stunden ab.', createInvitation: 'Einladung erstellen', footerPrivacy: 'Das Relay leitet ausschließlich Ende-zu-Ende-verschlüsselte Daten weiter.', sourceCode: 'Quellcode', loginTitle: 'Am Relay anmelden', loginDescription: 'Melde dich mit deinem Relay-Konto an.', setupTitle: 'Relay einrichten', setupDescription: 'Erstelle das erste Administratorkonto mit dem privaten Einrichtungscode des Servers.', redeemTitle: 'Einladung annehmen', redeemDescription: 'Erstelle mit deinem einmaligen Einladungscode ein Relay-Konto.', invalidCredentials: 'Anmeldung fehlgeschlagen. Prüfe Benutzername und Passwort.', setupDone: 'Die Ersteinrichtung wurde bereits abgeschlossen.', setupUnavailable: 'Der Einrichtungscode ist auf diesem Server nicht konfiguriert.', invalidSetup: 'Der Einrichtungscode ist ungültig.', invalidInvite: 'Der Einladungscode ist ungültig, abgelaufen oder bereits verwendet.', invalidAccount: 'Benutzername oder Passwort entspricht nicht den Vorgaben. Das Passwort muss mindestens 12 Zeichen haben.', loggedIn: 'Angemeldet.', loggedOut: 'Abgemeldet.', sessionExpired: 'Sitzung abgelaufen. Bitte erneut anmelden.', noAccount: 'Unbekanntes Konto', admin: 'Administrator', member: 'Mitglied', revoke: 'Entziehen', noAccounts: 'Keine weiteren Konten.', noInvitations: 'Keine offenen Einladungen.', used: 'verwendet', expires: 'Läuft ab', invitationCreated: 'Einladung erstellt. Teile diesen Code nur mit der eingeladenen Person:', revokeConfirm: 'Zugang für dieses Konto entziehen? Zugehörige Pairings werden ebenfalls beendet.', revokeInviteConfirm: 'Diese Einladung widerrufen?', deviceInvalid: 'Der Code ist ungültig oder abgelaufen. Starte die Relay-Anmeldung in der Mac-App erneut.', deviceAuthorized: 'Mac wurde autorisiert. Wechsle zurück zur Meets-App.', deviceFailed: 'Die Freigabe konnte nicht abgeschlossen werden.', loginFirst: 'Melde dich zuerst mit deinem Relay-Konto an.', devicePending: 'Bestätige die Anmeldung für diesen Mac.', setupCodeRequired: 'Der private Einrichtungscode wird benötigt.', invitationRequired: 'Der Einladungscode wird benötigt.', currentAccount: 'Du kannst das gerade verwendete Konto nicht selbst entziehen.', loadError: 'Daten konnten nicht geladen werden.' }, en: { secureRelay: 'Secure relay', privateByDesign: 'PRIVATE BY DESIGN', title: 'Your relay. Your connection.', intro: 'Manage access to your Meets Relay. Recordings and transcripts are never stored here.', signOut: 'Sign out', signIn: 'Sign in', invitation: 'Invitation', firstSetup: 'Initial setup', setupCode: 'Setup code', inviteCode: 'Invitation code', username: 'Username', password: 'Password', macSetup: 'MAC SETUP', authorizeMac: 'Connect this Mac to the relay?', authorizeDescription: 'After you approve, Meets on this Mac receives relay access. The browser never sends access tokens to the app.', authorize: 'Authorize Mac', returnMac: 'Return to Meets', adminConsole: 'RELAY ACCOUNT', pendingMac: 'Mac waiting for approval', accounts: 'Accounts', accountsHint: 'Members with relay access', invitations: 'Invitations', invitationsHint: 'One-time codes expire after 24 hours.', createInvitation: 'Create invitation', footerPrivacy: 'The relay forwards end-to-end encrypted data only.', sourceCode: 'Source code', loginTitle: 'Sign in to the relay', loginDescription: 'Sign in with your relay account.', setupTitle: 'Set up the relay', setupDescription: 'Create the first administrator account using the server’s private setup code.', redeemTitle: 'Accept invitation', redeemDescription: 'Create a relay account with your one-time invitation code.', invalidCredentials: 'Sign-in failed. Check your username and password.', setupDone: 'Initial setup has already been completed.', setupUnavailable: 'The setup code is not configured on this server.', invalidSetup: 'The setup code is invalid.', invalidInvite: 'The invitation code is invalid, expired, or already used.', invalidAccount: 'The username or password does not meet the requirements. Passwords need at least 12 characters.', loggedIn: 'Signed in.', loggedOut: 'Signed out.', sessionExpired: 'Session expired. Please sign in again.', noAccount: 'Unknown account', admin: 'Administrator', member: 'Member', revoke: 'Revoke', noAccounts: 'No other accounts.', noInvitations: 'No open invitations.', used: 'used', expires: 'Expires', invitationCreated: 'Invitation created. Share this code only with the invited person:', revokeConfirm: 'Revoke access for this account? Its pairings will also be ended.', revokeInviteConfirm: 'Revoke this invitation?', deviceInvalid: 'The code is invalid or expired. Restart relay sign-in in the Mac app.', deviceAuthorized: 'Mac authorized. Return to Meets.', deviceFailed: 'Authorization could not be completed.', loginFirst: 'Sign in to your relay account first.', devicePending: 'Approve the sign-in for this Mac.', setupCodeRequired: 'The private setup code is required.', invitationRequired: 'The invitation code is required.', currentAccount: 'You cannot revoke the account currently in use.', loadError: 'Could not load data.' } }; let language = localStorage.getItem('meets-relay-language') || (navigator.language.toLowerCase().startsWith('de') ? 'de' : 'en'); let mode = 'login'; let account = null; let deviceCode = ''; let callbackState = ''; const t = key => translations[language][key] || key; const $ = selector => document.querySelector(selector); const escapeText = value => String(value ?? '').replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]); function setLanguage(next) { language = next; localStorage.setItem('meets-relay-language', language); document.documentElement.lang = language; document.querySelectorAll('[data-i18n]').forEach(node => { node.textContent = t(node.dataset.i18n); }); $('#language').textContent = language === 'de' ? 'DE / EN' : 'EN / DE'; updateAuthMode(); } async function api(path, options = {}) { const response = await fetch(path, { credentials: 'same-origin', cache: 'no-store', ...options, headers: { ...(options.body ? { 'content-type': 'application/json' } : {}), ...(options.headers || {}) }, body: options.body ? JSON.stringify(options.body) : undefined }); const body = await response.json().catch(() => ({})); if (!response.ok) throw Object.assign(new Error(body.error || 'request_failed'), { status: response.status }); return body; } function showMessage(element, message, isError = false) { element.textContent = message || ''; element.classList.toggle('error', isError); } function updateAuthMode() { const title = mode === 'setup' ? 'setupTitle' : mode === 'redeem' ? 'redeemTitle' : 'loginTitle'; const description = mode === 'setup' ? 'setupDescription' : mode === 'redeem' ? 'redeemDescription' : 'loginDescription'; $('#auth-title').textContent = t(title); $('#auth-description').textContent = t(description); $('#auth-submit').textContent = t(mode === 'setup' ? 'firstSetup' : mode === 'redeem' ? 'invitation' : 'signIn'); $('#setup-code-wrap').classList.toggle('hidden', mode !== 'setup'); $('#invite-code-wrap').classList.toggle('hidden', mode !== 'redeem'); $('#auth-form').elements.password.autocomplete = mode === 'login' ? 'current-password' : 'new-password'; document.querySelectorAll('.auth-tabs button').forEach(button => button.classList.toggle('active', button.dataset.mode === mode)); } function customError(error) { const errors = { invalid_credentials: 'invalidCredentials', setup_already_completed: 'setupDone', relay_setup_code_not_configured: 'setupUnavailable', invalid_setup_code: 'invalidSetup', invalid_invitation: 'invalidInvite', invalid_account_credentials: 'invalidAccount', unauthorized: 'sessionExpired', cannot_revoke_current_account: 'currentAccount' }; return t(errors[error.message] || 'loadError'); } function toggleAuth(cardMode) { $('#auth-card').classList.toggle('hidden', cardMode === 'device' || Boolean(account)); $('#console-card').classList.toggle('hidden', !account); $('#logout').classList.toggle('hidden', !account); if (cardMode && ['login', 'setup', 'redeem'].includes(cardMode)) { mode = cardMode; updateAuthMode(); } if (account) { $('#welcome').textContent = `${account.username}`; $('#role').textContent = t(account.role === 'admin' ? 'admin' : 'member'); const isAdmin = account.role === 'admin'; document.querySelectorAll('.console-section').forEach(section => section.classList.toggle('hidden', !isAdmin)); if (isAdmin) refreshAdmin().catch(error => showMessage($('#console-message'), customError(error), true)); } } async function loadSession() { try { const response = await api('/api/v1/web/session'); account = response.account; toggleAuth(); } catch { account = null; const status = await api('/api/v1/web/status'); document.querySelector('[data-mode="setup"]').classList.toggle('hidden', status.setupCompleted); mode = status.setupCompleted ? 'login' : 'setup'; toggleAuth(mode); } } async function refreshAdmin() { const [accounts, invitations] = await Promise.all([ api('/api/v1/web/admin/accounts'), api('/api/v1/web/admin/invitations') ]); const others = accounts.accounts.filter(item => item.id !== account.id); $('#accounts').replaceChildren(); if (!others.length) $('#accounts').innerHTML = `
${escapeText(invitation.code)}`;
output.classList.remove('hidden');
output.querySelector('button').addEventListener('click', async () => { await navigator.clipboard.writeText(invitation.code); });
await refreshAdmin();
} catch (error) { showMessage($('#console-message'), customError(error), true); }
});
$('#return-mac').addEventListener('click', () => {
const state = $('#return-mac').dataset.state;
if (state) location.href = `audiorecordermac://relay-setup?state=${encodeURIComponent(state)}`;
});
setLanguage(language);
const isDevicePage = devicePage();
loadSession().then(() => {
if (isDevicePage && account) showMessage($('#device-message'), t('devicePending'));
}).catch(error => showMessage($('#auth-message'), customError(error), true));