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 {
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;
+16 -9
View File
@@ -30,20 +30,25 @@ export function bootstrapApp() {
outlet: document.querySelector('#route-view'),
});
window.__loncApp = {
navigate,
async refreshKitchens() {
const kitchens = await listKitchens(store);
function applyKitchens(kitchens) {
store.setKitchens(kitchens);
if (!store.activeKitchen && kitchens.length) {
store.setActiveKitchen(kitchens[0]);
}
return kitchens;
}
window.__loncApp = {
navigate,
async refreshKitchens() {
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);
+9 -1
View File
@@ -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,
+18 -3
View File
@@ -148,11 +148,14 @@ export function loginPageData(store) {
userLogin: this.form.userLogin.trim(),
});
this.syncFromStore();
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 = '';
}
},
};
}