upend/webui/src/lib/api.ts

30 lines
938 B
TypeScript

import { UpEndApi } from '@upnd/upend';
import { UpEndWasmExtensionsWeb } from '@upnd/upend/wasm/web';
import wasmURL from '@upnd/wasm-web/upend_wasm_bg.wasm?url';
import { type StartStopNotifier, writable, type Writable } from 'svelte/store';
import * as Sentry from '@sentry/sveltekit';
const wasm = new UpEndWasmExtensionsWeb(wasmURL);
const api = new UpEndApi({ instanceUrl: '/', wasmExtensions: wasm });
export default api;
export const currentUser: Writable<string | undefined> = writable(
undefined as string | undefined,
((set) => {
api.authStatus().then((result) => {
set(result?.user);
Sentry.setUser({ id: result?.user });
});
}) as StartStopNotifier<string | undefined>
);
export async function login(credentials: { username: string; password: string }) {
await api.authenticate(credentials);
window.location.reload();
}
export async function logout() {
await api.resetAuth();
window.location.reload();
}