diff --git a/src/api/auth.js b/src/api/auth.js index 8e8770d..5c37109 100644 --- a/src/api/auth.js +++ b/src/api/auth.js @@ -46,14 +46,14 @@ export async function restoreSession(store) { } try { - await verifyConnection(store); + return await verifyConnection(store); } catch (error) { if (!isAuthFailure(error)) { throw error; } } - return store.session; + return { session: store.session, kitchens: [] }; } export async function logout(store) { @@ -80,13 +80,16 @@ export async function verifyConnection(store) { } try { - await listKitchens(store); + const kitchens = await listKitchens(store); store.setSession({ ...store.session, state: CONNECTION_STATES.connected, hasValidated: true, }); - return store.session; + return { + session: store.session, + kitchens, + }; } catch (error) { if (!isAuthFailure(error)) { throw error; diff --git a/src/app/bootstrap.js b/src/app/bootstrap.js index cb86d6a..2e9852d 100644 --- a/src/app/bootstrap.js +++ b/src/app/bootstrap.js @@ -30,20 +30,25 @@ export function bootstrapApp() { outlet: document.querySelector('#route-view'), }); + function applyKitchens(kitchens) { + store.setKitchens(kitchens); + if (!store.activeKitchen && kitchens.length) { + store.setActiveKitchen(kitchens[0]); + } + return kitchens; + } + window.__loncApp = { navigate, async refreshKitchens() { - const kitchens = await listKitchens(store); - store.setKitchens(kitchens); - if (!store.activeKitchen && kitchens.length) { - store.setActiveKitchen(kitchens[0]); - } - return kitchens; + return applyKitchens(await listKitchens(store)); }, async restoreSession() { try { - await restoreSession(store); - if (store.isConnected) { + const result = await restoreSession(store); + if (result?.kitchens) { + applyKitchens(result.kitchens); + } else if (store.isConnected) { await window.__loncApp.refreshKitchens(); } } catch (error) { @@ -53,11 +58,13 @@ export function bootstrapApp() { } }, async verifyConnection() { - await verifyConnection(store); - if (store.isConnected) { + const result = await verifyConnection(store); + if (result?.kitchens) { + applyKitchens(result.kitchens); + } else if (store.isConnected) { await window.__loncApp.refreshKitchens(); } - return store.session; + return result; }, async logout() { await logout(store); diff --git a/src/app/store.js b/src/app/store.js index 45755ce..49132b0 100644 --- a/src/app/store.js +++ b/src/app/store.js @@ -18,6 +18,14 @@ const defaultState = { alerts: [], }; +function createAlertId() { + if (typeof globalThis !== 'undefined' && globalThis.crypto?.randomUUID) { + return globalThis.crypto.randomUUID(); + } + + return `alert-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`; +} + export function createAppStore() { const state = { ...defaultState, @@ -80,7 +88,7 @@ export function createAppStore() { }, addAlert(alert) { const nextAlert = { - id: crypto.randomUUID(), + id: createAlertId(), type: 'info', timeout: 5000, ...alert, diff --git a/src/features/auth/login-page.js b/src/features/auth/login-page.js index 6e40e56..b667e43 100644 --- a/src/features/auth/login-page.js +++ b/src/features/auth/login-page.js @@ -148,11 +148,14 @@ export function loginPageData(store) { userLogin: this.form.userLogin.trim(), }); this.syncFromStore(); - - store.addAlert({ - type: 'info', - message: `Got secret key starting ${store.session.applicationKey.slice(0, 8)}.... Approve this key in Tryton preferences.`, - }); + try { + store.addAlert({ + type: 'info', + message: `Got secret key starting ${store.session.applicationKey.slice(0, 8)}.... Approve this key in Tryton preferences.`, + }); + } catch (error) { + console.warn('Alert display failed after successful key creation.', error); + } }); }, async checkConnection() { @@ -160,12 +163,20 @@ export function loginPageData(store) { await window.__loncApp.verifyConnection(); this.syncFromStore(); if (store.isConnected) { + this.verifyState.error = ''; store.addAlert({ type: 'success', message: 'Kitchen application key is active.' }); navigate('/'); } - }).catch(() => { + }).catch((error) => { + if (store.isConnected) { + this.verifyState.error = ''; + return; + } + const status = error?.cause?.status || error?.status; this.verifyState.error = - 'Failed to verify connection. Please verify the application key in Tryton first.'; + status === 403 + ? 'Failed to verify connection. Please verify the application key in Tryton first.' + : (error?.message || this.verifyState.error || 'Failed to verify connection.'); }); }, async tryAutoVerify() { @@ -179,6 +190,7 @@ export function loginPageData(store) { this.syncFromStore(); if (store.isConnected) { + this.verifyState.error = ''; await window.__loncApp.refreshKitchens(); navigate('/'); } @@ -223,6 +235,9 @@ export function loginPageData(store) { this.sessionState = store.session?.state || CONNECTION_STATES.notConnected; this.applicationKey = store.session?.applicationKey || ''; this.form.userLogin = store.session?.userLogin || this.form.userLogin; + if (this.sessionState === CONNECTION_STATES.connected) { + this.verifyState.error = ''; + } }, }; }