112 lines
2.6 KiB
JavaScript
112 lines
2.6 KiB
JavaScript
|
|
import { CONNECTION_STATES, TRYTON_APPLICATION } from '../app/config.js';
|
||
|
|
import { apiRequest, getPath } from './client.js';
|
||
|
|
import { listKitchens } from './kitchens.js';
|
||
|
|
|
||
|
|
function extractKey(payload) {
|
||
|
|
if (typeof payload === 'string') {
|
||
|
|
return payload;
|
||
|
|
}
|
||
|
|
|
||
|
|
return payload?.key || payload?.application_key || payload?.data?.key || null;
|
||
|
|
}
|
||
|
|
|
||
|
|
function isAuthFailure(error) {
|
||
|
|
const status = error?.cause?.status || error?.status;
|
||
|
|
return status === 401 || status === 403;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function login(store, credentials) {
|
||
|
|
const payload = await apiRequest(store, getPath('userApplication'), {
|
||
|
|
method: 'POST',
|
||
|
|
body: {
|
||
|
|
user: credentials.userLogin,
|
||
|
|
application: TRYTON_APPLICATION,
|
||
|
|
},
|
||
|
|
includeKitchen: false,
|
||
|
|
});
|
||
|
|
|
||
|
|
const applicationKey = extractKey(payload);
|
||
|
|
if (!applicationKey) {
|
||
|
|
throw new Error('User application creation did not return a key.');
|
||
|
|
}
|
||
|
|
|
||
|
|
const session = {
|
||
|
|
userLogin: credentials.userLogin,
|
||
|
|
applicationKey,
|
||
|
|
state: CONNECTION_STATES.pendingValidation,
|
||
|
|
hasValidated: false,
|
||
|
|
};
|
||
|
|
store.setSession(session);
|
||
|
|
return session;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function restoreSession(store) {
|
||
|
|
if (!store.session) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await verifyConnection(store);
|
||
|
|
} catch (error) {
|
||
|
|
if (!isAuthFailure(error)) {
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return store.session;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function logout(store) {
|
||
|
|
try {
|
||
|
|
if (store.session?.applicationKey && store.session?.userLogin) {
|
||
|
|
await apiRequest(store, getPath('userApplication'), {
|
||
|
|
method: 'DELETE',
|
||
|
|
body: {
|
||
|
|
user: store.session.userLogin,
|
||
|
|
key: store.session.applicationKey,
|
||
|
|
application: TRYTON_APPLICATION,
|
||
|
|
},
|
||
|
|
includeKitchen: false,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
} finally {
|
||
|
|
store.clearSessionState();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function verifyConnection(store) {
|
||
|
|
if (!store.session?.applicationKey) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
await listKitchens(store);
|
||
|
|
store.setSession({
|
||
|
|
...store.session,
|
||
|
|
state: CONNECTION_STATES.connected,
|
||
|
|
hasValidated: true,
|
||
|
|
});
|
||
|
|
return store.session;
|
||
|
|
} catch (error) {
|
||
|
|
if (!isAuthFailure(error)) {
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
|
||
|
|
store.setSession({
|
||
|
|
...store.session,
|
||
|
|
state: store.session.hasValidated
|
||
|
|
? CONNECTION_STATES.invalidKey
|
||
|
|
: CONNECTION_STATES.pendingValidation,
|
||
|
|
});
|
||
|
|
|
||
|
|
throw new Error(
|
||
|
|
store.session.hasValidated
|
||
|
|
? 'The stored application key is no longer valid.'
|
||
|
|
: 'The application key is still waiting for validation in Tryton preferences.',
|
||
|
|
{
|
||
|
|
cause: error.cause || error,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|