From 0b2c03dc5e1060cd229aeafb84263f171b5a9788 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 25 Sep 2023 09:31:17 -0300 Subject: new libeufin api --- packages/demobank-ui/src/hooks/backend.ts | 43 +++++++------------------------ 1 file changed, 10 insertions(+), 33 deletions(-) (limited to 'packages/demobank-ui/src/hooks/backend.ts') diff --git a/packages/demobank-ui/src/hooks/backend.ts b/packages/demobank-ui/src/hooks/backend.ts index f2be90f08..e6a3a1c0c 100644 --- a/packages/demobank-ui/src/hooks/backend.ts +++ b/packages/demobank-ui/src/hooks/backend.ts @@ -40,6 +40,7 @@ import { useCallback, useEffect, useState } from "preact/hooks"; import { useSWRConfig } from "swr"; import { useBackendContext } from "../context/backend.js"; import { bankUiSettings } from "../settings.js"; +import { AccessToken } from "./useCredentialsChecker.js"; /** * Has the information to reach and @@ -49,7 +50,7 @@ export type BackendState = LoggedIn | LoggedOut; export interface BackendCredentials { username: string; - password: string; + token: AccessToken; } interface LoggedIn extends BackendCredentials { @@ -64,7 +65,7 @@ export const codecForBackendStateLoggedIn = (): Codec => buildCodecForObject() .property("status", codecForConstString("loggedIn")) .property("username", codecForString()) - .property("password", codecForString()) + .property("token", codecForString() as Codec) .property("isUserAdministrator", codecForBoolean()) .build("BackendState.LoggedIn"); @@ -255,35 +256,11 @@ interface InvalidationResult { error: unknown; } -export function useCredentialsCheckerOld() { - const { request } = useApiContext(); - const baseUrl = getInitialBackendBaseURL(); - //check against account details endpoint - //while sandbox backend doesn't have a login endpoint - return async function testLogin( - username: string, - password: string, - ): Promise { - try { - await request(baseUrl, `access-api/accounts/${username}/`, { - basicAuth: { username, password }, - preventCache: true, - }); - return { valid: true }; - } catch (error) { - if (error instanceof RequestError) { - return { valid: false, requestError: true, cause: error.cause }; - } - return { valid: false, requestError: false, error }; - } - }; -} - export function useAuthenticatedBackend(): useBackendType { const { state } = useBackendContext(); const { request: requestHandler } = useApiContext(); - const creds = state.status === "loggedIn" ? state : undefined; + const creds = state.status === "loggedIn" ? state.token : undefined; const baseUrl = getInitialBackendBaseURL(); const request = useCallback( @@ -291,14 +268,14 @@ export function useAuthenticatedBackend(): useBackendType { path: string, options: RequestOptions = {}, ): Promise> { - return requestHandler(baseUrl, path, { basicAuth: creds, ...options }); + return requestHandler(baseUrl, path, { token: creds, ...options }); }, [baseUrl, creds], ); const fetcher = useCallback( function fetcherImpl(endpoint: string): Promise> { - return requestHandler(baseUrl, endpoint, { basicAuth: creds }); + return requestHandler(baseUrl, endpoint, { token: creds }); }, [baseUrl, creds], ); @@ -309,7 +286,7 @@ export function useAuthenticatedBackend(): useBackendType { number, ]): Promise> { return requestHandler(baseUrl, endpoint, { - basicAuth: creds, + token: creds, params: { delta: size, start: size * page }, }); }, @@ -321,7 +298,7 @@ export function useAuthenticatedBackend(): useBackendType { > { return Promise.all( endpoints.map((endpoint) => - requestHandler(baseUrl, endpoint, { basicAuth: creds }), + requestHandler(baseUrl, endpoint, { token: creds }), ), ); }, @@ -335,7 +312,7 @@ export function useAuthenticatedBackend(): useBackendType { string, ]): Promise> { return requestHandler(baseUrl, endpoint, { - basicAuth: creds, + token: creds, params: { page: page || 1, size }, }); }, @@ -347,7 +324,7 @@ export function useAuthenticatedBackend(): useBackendType { HttpResponseOk > { return requestHandler(baseUrl, endpoint, { - basicAuth: creds, + token: creds, params: { account }, }); }, -- cgit v1.2.3