Fix auth verification flow and alert ID fallback

This commit is contained in:
2026-04-06 17:07:14 +02:00
parent 1521602743
commit a0da39b697
4 changed files with 56 additions and 23 deletions
+7 -4
View File
@@ -46,14 +46,14 @@ export async function restoreSession(store) {
} }
try { try {
await verifyConnection(store); return await verifyConnection(store);
} catch (error) { } catch (error) {
if (!isAuthFailure(error)) { if (!isAuthFailure(error)) {
throw error; throw error;
} }
} }
return store.session; return { session: store.session, kitchens: [] };
} }
export async function logout(store) { export async function logout(store) {
@@ -80,13 +80,16 @@ export async function verifyConnection(store) {
} }
try { try {
await listKitchens(store); const kitchens = await listKitchens(store);
store.setSession({ store.setSession({
...store.session, ...store.session,
state: CONNECTION_STATES.connected, state: CONNECTION_STATES.connected,
hasValidated: true, hasValidated: true,
}); });
return store.session; return {
session: store.session,
kitchens,
};
} catch (error) { } catch (error) {
if (!isAuthFailure(error)) { if (!isAuthFailure(error)) {
throw error; throw error;
+18 -11
View File
@@ -30,20 +30,25 @@ export function bootstrapApp() {
outlet: document.querySelector('#route-view'), outlet: document.querySelector('#route-view'),
}); });
function applyKitchens(kitchens) {
store.setKitchens(kitchens);
if (!store.activeKitchen && kitchens.length) {
store.setActiveKitchen(kitchens[0]);
}
return kitchens;
}
window.__loncApp = { window.__loncApp = {
navigate, navigate,
async refreshKitchens() { async refreshKitchens() {
const kitchens = await listKitchens(store); return applyKitchens(await listKitchens(store));
store.setKitchens(kitchens);
if (!store.activeKitchen && kitchens.length) {
store.setActiveKitchen(kitchens[0]);
}
return kitchens;
}, },
async restoreSession() { async restoreSession() {
try { try {
await restoreSession(store); const result = await restoreSession(store);
if (store.isConnected) { if (result?.kitchens) {
applyKitchens(result.kitchens);
} else if (store.isConnected) {
await window.__loncApp.refreshKitchens(); await window.__loncApp.refreshKitchens();
} }
} catch (error) { } catch (error) {
@@ -53,11 +58,13 @@ export function bootstrapApp() {
} }
}, },
async verifyConnection() { async verifyConnection() {
await verifyConnection(store); const result = await verifyConnection(store);
if (store.isConnected) { if (result?.kitchens) {
applyKitchens(result.kitchens);
} else if (store.isConnected) {
await window.__loncApp.refreshKitchens(); await window.__loncApp.refreshKitchens();
} }
return store.session; return result;
}, },
async logout() { async logout() {
await logout(store); await logout(store);
+9 -1
View File
@@ -18,6 +18,14 @@ const defaultState = {
alerts: [], 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() { export function createAppStore() {
const state = { const state = {
...defaultState, ...defaultState,
@@ -80,7 +88,7 @@ export function createAppStore() {
}, },
addAlert(alert) { addAlert(alert) {
const nextAlert = { const nextAlert = {
id: crypto.randomUUID(), id: createAlertId(),
type: 'info', type: 'info',
timeout: 5000, timeout: 5000,
...alert, ...alert,
+22 -7
View File
@@ -148,11 +148,14 @@ export function loginPageData(store) {
userLogin: this.form.userLogin.trim(), userLogin: this.form.userLogin.trim(),
}); });
this.syncFromStore(); this.syncFromStore();
try {
store.addAlert({ store.addAlert({
type: 'info', type: 'info',
message: `Got secret key starting ${store.session.applicationKey.slice(0, 8)}.... Approve this key in Tryton preferences.`, 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() { async checkConnection() {
@@ -160,12 +163,20 @@ export function loginPageData(store) {
await window.__loncApp.verifyConnection(); await window.__loncApp.verifyConnection();
this.syncFromStore(); this.syncFromStore();
if (store.isConnected) { if (store.isConnected) {
this.verifyState.error = '';
store.addAlert({ type: 'success', message: 'Kitchen application key is active.' }); store.addAlert({ type: 'success', message: 'Kitchen application key is active.' });
navigate('/'); navigate('/');
} }
}).catch(() => { }).catch((error) => {
if (store.isConnected) {
this.verifyState.error = '';
return;
}
const status = error?.cause?.status || error?.status;
this.verifyState.error = 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() { async tryAutoVerify() {
@@ -179,6 +190,7 @@ export function loginPageData(store) {
this.syncFromStore(); this.syncFromStore();
if (store.isConnected) { if (store.isConnected) {
this.verifyState.error = '';
await window.__loncApp.refreshKitchens(); await window.__loncApp.refreshKitchens();
navigate('/'); navigate('/');
} }
@@ -223,6 +235,9 @@ export function loginPageData(store) {
this.sessionState = store.session?.state || CONNECTION_STATES.notConnected; this.sessionState = store.session?.state || CONNECTION_STATES.notConnected;
this.applicationKey = store.session?.applicationKey || ''; this.applicationKey = store.session?.applicationKey || '';
this.form.userLogin = store.session?.userLogin || this.form.userLogin; this.form.userLogin = store.session?.userLogin || this.form.userLogin;
if (this.sessionState === CONNECTION_STATES.connected) {
this.verifyState.error = '';
}
}, },
}; };
} }