taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit c90739b6a13ffa7fa1e892ad71d6bc22d722b318
parent 0ee947c13bd8f6cf06d7a22a708e49c85fcb1c73
Author: Sebastian <sebasjm@gmail.com>
Date:   Thu,  6 Mar 2025 09:53:37 -0300

do not override AC state, just make the URL higher priority unless we are on debug mode

Diffstat:
Mpackages/kyc-ui/src/Routing.tsx | 51+++------------------------------------------------
Mpackages/kyc-ui/src/hooks/session.ts | 40+++++++++++++++++++++++++++++++++++++++-
Mpackages/kyc-ui/src/pages/TriggerKyc.tsx | 2--
3 files changed, 42 insertions(+), 51 deletions(-)

diff --git a/packages/kyc-ui/src/Routing.tsx b/packages/kyc-ui/src/Routing.tsx @@ -59,14 +59,6 @@ const publicPages = { ), }; -function safeGetParam( - ps: Record<string, string[]>, - n: string, -): string | undefined { - if (!ps[n] || ps[n].length == 0) return undefined; - return ps[n][0]; -} - export function safeToURL(s: string | undefined): URL | undefined { if (s === undefined) return undefined; try { @@ -76,31 +68,6 @@ export function safeToURL(s: string | undefined): URL | undefined { } } -const ACCESS_TOKEN_REGEX = new RegExp("[A-Z0-9]{52}"); - -/** - * by how the exchange serves the SPA - * /kyc-spa/KXAFXEWM7E3EJSYD9GJ30FYK1C17AKZWV119ZJA3XGPBBMZFJ2C0#/start - * - * by how dev.mjs serves the SPA - * /app/?token=KXAFXEWM7E3EJSYD9GJ30FYK1C17AKZWV119ZJA3XGPBBMZFJ2C0#/start - * @returns - */ -function getAccessTokenFromURL(): AccessToken | undefined { - if (typeof window === "undefined") return undefined; - const paths = window.location.pathname.split("/"); - if (paths.length < 3) return undefined; - const path = paths[2]; - if (path && ACCESS_TOKEN_REGEX.test(path)) { - return path as AccessToken; - } - const param = new URLSearchParams(window.location.search).get("token"); - if (param && ACCESS_TOKEN_REGEX.test(param)) { - return param as AccessToken; - } - return undefined; -} - function PublicRounting(): VNode { const location = useCurrentLocation(publicPages); const { state, start } = useSessionState(); @@ -108,32 +75,20 @@ function PublicRounting(): VNode { useErrorBoundary((e) => { console.log("error", e); }); - const sessionToken = state?.accessToken; - const urlToken = getAccessTokenFromURL(); - useEffect(() => { - if (!urlToken) { - //special case, loading without URL the it should use the session - return; - } - // loading a new session - if (urlToken !== sessionToken) { - start(urlToken); - } - }, [sessionToken, urlToken]); - + const currentToken = state?.accessToken; switch (location.name) { case undefined: { navigateTo(publicPages.start.url({})); return <Loading />; } case "start": { - if (!sessionToken) { + if (!currentToken) { return <div>No access token</div>; } return ( <Start - token={sessionToken} + token={currentToken} onLoggedOut={() => { navigateTo(publicPages.completed.url({})); }} diff --git a/packages/kyc-ui/src/hooks/session.ts b/packages/kyc-ui/src/hooks/session.ts @@ -25,6 +25,7 @@ import { codecOptionalDefault, } from "@gnu-taler/taler-util"; import { buildStorageKey, useLocalStorage } from "@gnu-taler/web-util/browser"; +import { usePreferences } from "../context/preferences.js"; export type SessionState = { accessToken: AccessToken; @@ -67,11 +68,48 @@ const SESSION_STATE_KEY = buildStorageKey( */ export function useSessionState(): SessionStateHandler { const { value: state, update } = useLocalStorage(SESSION_STATE_KEY); + const [pref] = usePreferences(); + const urlToken = getAccessTokenFromURL(); + // if the user access the SPA with a token in the URL then + // don't use the session saved in the storage + // unless we are on debugMode. Using debug mode will not + // use the URL token in order to switch session easily return { - state, + state: !state + ? undefined + : { + accessToken: pref.showDebugInfo + ? state.accessToken + : urlToken ?? state.accessToken, + testAccounts: state.testAccounts, + }, start(accessToken) { update({ accessToken, testAccounts: [] }); }, }; } + +const ACCESS_TOKEN_REGEX = new RegExp("[A-Z0-9]{52}"); +/** + * by how the exchange serves the SPA + * /kyc-spa/KXAFXEWM7E3EJSYD9GJ30FYK1C17AKZWV119ZJA3XGPBBMZFJ2C0#/start + * + * by how dev.mjs serves the SPA + * /app/?token=KXAFXEWM7E3EJSYD9GJ30FYK1C17AKZWV119ZJA3XGPBBMZFJ2C0#/start + * @returns + */ +function getAccessTokenFromURL(): AccessToken | undefined { + if (typeof window === "undefined") return undefined; + const paths = window.location.pathname.split("/"); + if (paths.length < 3) return undefined; + const path = paths[2]; + if (path && ACCESS_TOKEN_REGEX.test(path)) { + return path as AccessToken; + } + const param = new URLSearchParams(window.location.search).get("token"); + if (param && ACCESS_TOKEN_REGEX.test(param)) { + return param as AccessToken; + } + return undefined; +} diff --git a/packages/kyc-ui/src/pages/TriggerKyc.tsx b/packages/kyc-ui/src/pages/TriggerKyc.tsx @@ -35,7 +35,6 @@ import { } from "@gnu-taler/web-util/browser"; import { Fragment, h, VNode } from "preact"; import { useEffect, useMemo, useState } from "preact/hooks"; -import { useSessionState } from "../hooks/session.js"; const TALER_SCREEN_ID = 102; @@ -47,7 +46,6 @@ type Props = { }; export function TriggerKyc({ onKycStarted }: Props): VNode { const { i18n } = useTranslationContext(); - const { state } = useSessionState(); const [notification, withErrorHandler, notify] = useLocalNotificationHandler(); const { config, lib } = useExchangeApiContext();