ask password logic & modal

This commit is contained in:
Jan Prochazka
2022-12-25 10:21:19 +01:00
parent fa13990189
commit d66fc06403
8 changed files with 176 additions and 5 deletions

View File

@@ -5,6 +5,9 @@ import getElectron from './getElectron';
// import socket from './socket';
import { showSnackbarError } from '../utility/snackbar';
import { isOauthCallback, redirectToLogin } from '../clientAuth';
import { showModal } from '../modals/modalTools';
import DatabaseLoginModal, { isDatabaseLoginVisible } from '../modals/DatabaseLoginModal.svelte';
import _ from 'lodash';
let eventSource;
let apiLogging = false;
@@ -12,6 +15,8 @@ let apiLogging = false;
let apiDisabled = false;
const disabledOnOauth = isOauthCallback();
const volatileConnectionMap = {};
export function disableApi() {
apiDisabled = true;
}
@@ -20,6 +25,10 @@ export function enableApi() {
apiDisabled = false;
}
export function setVolatileConnectionRemapping(existingConnectionId, volatileConnectionId) {
volatileConnectionMap[existingConnectionId] = volatileConnectionId;
}
function wantEventSource() {
if (!eventSource) {
eventSource = new EventSource(`${resolveApi()}/stream`);
@@ -32,7 +41,16 @@ function processApiResponse(route, args, resp) {
// console.log('<<< API RESPONSE', route, args, resp);
// }
if (resp?.apiErrorMessage) {
if (resp?.missingCredentials) {
if (!isDatabaseLoginVisible()) {
showModal(DatabaseLoginModal, resp.detail);
}
return null;
// return {
// errorMessage: resp.apiErrorMessage,
// missingCredentials: true,
// };
} else if (resp?.apiErrorMessage) {
showSnackbarError('API error:' + resp?.apiErrorMessage);
return {
errorMessage: resp.apiErrorMessage,
@@ -42,6 +60,10 @@ function processApiResponse(route, args, resp) {
return resp;
}
function transformApiArgs(args) {
return _.mapValues(args, (v, k) => (k == 'conid' && v && volatileConnectionMap[v] ? volatileConnectionMap[v] : v));
}
export async function apiCall(route: string, args: {} = undefined) {
if (apiLogging) {
console.log('>>> API CALL', route, args);
@@ -55,6 +77,8 @@ export async function apiCall(route: string, args: {} = undefined) {
return;
}
args = transformApiArgs(args);
const electron = getElectron();
if (electron) {
const resp = await electron.invoke(route.replace('/', '-'), args);