Fix auth verification flow and alert ID fallback
This commit is contained in:
+7
-4
@@ -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;
|
||||||
|
|||||||
Vendored
+16
-9
@@ -30,20 +30,25 @@ export function bootstrapApp() {
|
|||||||
outlet: document.querySelector('#route-view'),
|
outlet: document.querySelector('#route-view'),
|
||||||
});
|
});
|
||||||
|
|
||||||
window.__loncApp = {
|
function applyKitchens(kitchens) {
|
||||||
navigate,
|
|
||||||
async refreshKitchens() {
|
|
||||||
const kitchens = await listKitchens(store);
|
|
||||||
store.setKitchens(kitchens);
|
store.setKitchens(kitchens);
|
||||||
if (!store.activeKitchen && kitchens.length) {
|
if (!store.activeKitchen && kitchens.length) {
|
||||||
store.setActiveKitchen(kitchens[0]);
|
store.setActiveKitchen(kitchens[0]);
|
||||||
}
|
}
|
||||||
return kitchens;
|
return kitchens;
|
||||||
|
}
|
||||||
|
|
||||||
|
window.__loncApp = {
|
||||||
|
navigate,
|
||||||
|
async refreshKitchens() {
|
||||||
|
return applyKitchens(await listKitchens(store));
|
||||||
},
|
},
|
||||||
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
@@ -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,
|
||||||
|
|||||||
@@ -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 = '';
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user