integreat
Version:
Node.js integration layer
40 lines • 1.65 kB
JavaScript
import { isInternalIdent } from './is.js';
import { setResponseOnAction } from './action.js';
import { createErrorResponse } from './response.js';
const createGetIdentAction = (ident) => ({
type: 'GET_IDENT',
payload: {},
meta: { ident, cache: true },
});
const isIdentCompletable = (ident) => !!ident &&
!!(ident.id || ident.withToken) &&
!ident.isCompleted &&
!isInternalIdent(ident);
export async function completeIdent(originalIdent, dispatch) {
if (!isIdentCompletable(originalIdent)) {
return { status: 'ok', access: { ident: originalIdent } };
}
const response = await dispatch(createGetIdentAction(originalIdent));
if (response.status === 'ok') {
const ident = response?.access?.ident || originalIdent;
return { status: 'ok', access: { ident } };
}
else if (response.status === 'notfound') {
return createErrorResponse(`Ident '${originalIdent.id}' was not found. [${response.status}] ${response.error}`, 'auth:ident', 'noaccess', 'unknownident');
}
else {
return createErrorResponse(`Could not fetch ident '${originalIdent.id}'. [${response.status}] ${response.error}`, 'auth:ident', 'autherror');
}
}
export async function completeIdentOnAction(action, dispatch) {
const originalIdent = action.meta?.ident;
const response = await completeIdent(originalIdent, dispatch);
if (response.status === 'ok') {
const ident = response?.access?.ident;
return { ...action, meta: { ...action.meta, ident } };
}
else {
return setResponseOnAction(action, response);
}
}
//# sourceMappingURL=completeIdent.js.map