From 4de014927e95d792633ea367eb4404459489d44f Mon Sep 17 00:00:00 2001
From: Sebastian
Date: Sun, 5 Mar 2023 15:21:12 -0300
Subject: [PATCH] validate IBAN, removing internal iban from account form, add
missing logo, do not save backend URL in login state
---
copy-demobank-into-prebuilt.sh | 2 +-
packages/demobank-ui/README.md | 2 +
packages/demobank-ui/src/hooks/backend.ts | 16 +-
packages/demobank-ui/src/hooks/circuit.ts | 10 +-
packages/demobank-ui/src/pages/AdminPage.tsx | 214 ++++++++++---------
packages/demobank-ui/src/utils.ts | 205 ++++++++++++++++++
6 files changed, 339 insertions(+), 110 deletions(-)
diff --git a/copy-demobank-into-prebuilt.sh b/copy-demobank-into-prebuilt.sh
index 5927eac96..3fd4ec5c5 100755
--- a/copy-demobank-into-prebuilt.sh
+++ b/copy-demobank-into-prebuilt.sh
@@ -2,7 +2,7 @@
[ ! -d prebuilt ] && echo 'directory "prebuilt" not found. first checkout the prebuilt branch into a prebuilt directory' && exit 1
-for file in index.html index.js index.css; do
+for file in index.html index.js index.css logo-white-U55BSKA2.svg; do
cp packages/demobank-ui/dist/$file prebuilt/demobank/
done
diff --git a/packages/demobank-ui/README.md b/packages/demobank-ui/README.md
index b8f96c5ea..1732b5f38 100644
--- a/packages/demobank-ui/README.md
+++ b/packages/demobank-ui/README.md
@@ -18,6 +18,7 @@ By default, the demobank-ui points to `https://bank.demo.taler.net/demobanks/def
as the bank access API base URL.
This can be changed for testing by setting the URL via local storage (via your browser's devtools):
+
```
localStorage.setItem("bank-base-url", OTHER_URL);
```
@@ -35,6 +36,7 @@ to the default settings:
```
globalThis.talerDemobankSettings = {
+ backendBaseURL: "https://bank.demo.taler.net/demobanks/default/",
allowRegistrations: true,
bankName: "Taler Bank",
// Show explainer text and navbar to other demo sites
diff --git a/packages/demobank-ui/src/hooks/backend.ts b/packages/demobank-ui/src/hooks/backend.ts
index 3f2981edf..3eaf1f186 100644
--- a/packages/demobank-ui/src/hooks/backend.ts
+++ b/packages/demobank-ui/src/hooks/backend.ts
@@ -42,26 +42,23 @@ export interface BackendCredentials {
}
interface LoggedIn extends BackendCredentials {
- url: string;
status: "loggedIn";
isUserAdministrator: boolean;
}
interface LoggedOut {
- url: string;
status: "loggedOut";
}
-const maybeRootPath = bankUiSettings.backendBaseURL;
-
export function getInitialBackendBaseURL(): string {
const overrideUrl = localStorage.getItem("bank-base-url");
- return canonicalizeBaseUrl(overrideUrl ? overrideUrl : maybeRootPath);
+ return canonicalizeBaseUrl(
+ overrideUrl ? overrideUrl : bankUiSettings.backendBaseURL,
+ );
}
export const defaultState: BackendState = {
status: "loggedOut",
- url: getInitialBackendBaseURL(),
};
export interface BackendStateHandler {
@@ -91,13 +88,12 @@ export function useBackendState(): BackendStateHandler {
return {
state,
logOut() {
- update(JSON.stringify({ ...defaultState, url: state.url }));
+ update(JSON.stringify({ ...defaultState }));
},
logIn(info) {
//admin is defined by the username
const nextState: BackendState = {
status: "loggedIn",
- url: state.url,
...info,
isUserAdministrator: info.username === "admin",
};
@@ -125,7 +121,7 @@ export function usePublicBackend(): useBackendType {
const { state } = useBackendContext();
const { request: requestHandler } = useApiContext();
- const baseUrl = state.url;
+ const baseUrl = getInitialBackendBaseURL();
const request = useCallback(
function requestImpl(
@@ -201,7 +197,7 @@ export function useAuthenticatedBackend(): useBackendType {
const { request: requestHandler } = useApiContext();
const creds = state.status === "loggedIn" ? state : undefined;
- const baseUrl = state.url;
+ const baseUrl = getInitialBackendBaseURL();
const request = useCallback(
function requestImpl(
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts
index c2563adb4..423ed1a5b 100644
--- a/packages/demobank-ui/src/hooks/circuit.ts
+++ b/packages/demobank-ui/src/hooks/circuit.ts
@@ -24,7 +24,11 @@ import {
import { useEffect, useMemo, useState } from "preact/hooks";
import { useBackendContext } from "../context/backend.js";
import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js";
-import { useAuthenticatedBackend, useMatchMutate } from "./backend.js";
+import {
+ getInitialBackendBaseURL,
+ useAuthenticatedBackend,
+ useMatchMutate,
+} from "./backend.js";
// FIX default import https://github.com/microsoft/TypeScript/issues/49189
import _useSWR, { SWRHook } from "swr";
@@ -210,10 +214,10 @@ export interface CircuitAccountAPI {
async function getBusinessStatus(
request: ReturnType["request"],
- url: string,
basicAuth: { username: string; password: string },
): Promise {
try {
+ const url = getInitialBackendBaseURL();
const result = await request<
HttpResponseOk
>(url, `circuit-api/accounts/${basicAuth.username}`, { basicAuth });
@@ -234,7 +238,7 @@ export function useBusinessAccountFlag(): boolean | undefined {
useEffect(() => {
if (!creds) return;
- getBusinessStatus(request, state.url, creds)
+ getBusinessStatus(request, creds)
.then((result) => {
setIsBusiness(result);
})
diff --git a/packages/demobank-ui/src/pages/AdminPage.tsx b/packages/demobank-ui/src/pages/AdminPage.tsx
index 2a5701a95..3d0c09cbf 100644
--- a/packages/demobank-ui/src/pages/AdminPage.tsx
+++ b/packages/demobank-ui/src/pages/AdminPage.tsx
@@ -40,6 +40,7 @@ import {
PartialButDefined,
RecursivePartial,
undefinedIfEmpty,
+ validateIBAN,
WithIntermediate,
} from "../utils.js";
import { ErrorBannerFloat } from "./BankFrame.js";
@@ -230,74 +231,78 @@ export function AdminPage({ onLoadNotOk }: Props): VNode {
-
- {i18n.str`Accounts:`}
-
-
+ {!customers.length ? (
+
+ ) : (
+
+ {i18n.str`Accounts:`}
+
+
+ )}
);
@@ -835,15 +840,15 @@ function AccountForm({
? i18n.str`only "IBAN" target are supported`
: !IBAN_REGEX.test(parsed.iban)
? i18n.str`IBAN should have just uppercased letters and numbers`
- : undefined,
+ : validateIBAN(parsed.iban, i18n),
contact_data: undefinedIfEmpty({
email: !newForm.contact_data?.email
- ? undefined
+ ? i18n.str`required`
: !EMAIL_REGEX.test(newForm.contact_data.email)
? i18n.str`it should be an email`
: undefined,
phone: !newForm.contact_data?.phone
- ? undefined
+ ? i18n.str`required`
: !newForm.contact_data.phone.startsWith("+")
? i18n.str`should start with +`
: !REGEX_JUST_NUMBERS_REGEX.test(newForm.contact_data.phone)
@@ -851,10 +856,10 @@ function AccountForm({
: undefined,
}),
iban: !newForm.iban
- ? i18n.str`required`
+ ? undefined //optional field
: !IBAN_REGEX.test(newForm.iban)
? i18n.str`IBAN should have just uppercased letters and numbers`
- : undefined,
+ : validateIBAN(newForm.iban, i18n),
name: !newForm.name ? i18n.str`required` : undefined,
username: !newForm.username ? i18n.str`required` : undefined,
});
@@ -866,7 +871,10 @@ function AccountForm({
return (