aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/hooks
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-11-18 09:55:23 -0300
committerSebastian <sebasjm@gmail.com>2023-11-18 09:55:23 -0300
commit7ed3e78f790837479fc2bb2eb6ddc40c78ce59b5 (patch)
treed914627537315e15240034fed441239448dfff07 /packages/aml-backoffice-ui/src/hooks
parentc797d551d9716924120d6ce6f270793c7bb5a4f9 (diff)
sync page with history
Diffstat (limited to 'packages/aml-backoffice-ui/src/hooks')
-rw-r--r--packages/aml-backoffice-ui/src/hooks/useOfficer.ts47
-rw-r--r--packages/aml-backoffice-ui/src/hooks/useSettings.ts10
2 files changed, 47 insertions, 10 deletions
diff --git a/packages/aml-backoffice-ui/src/hooks/useOfficer.ts b/packages/aml-backoffice-ui/src/hooks/useOfficer.ts
index 0747170e8..64cf79cc9 100644
--- a/packages/aml-backoffice-ui/src/hooks/useOfficer.ts
+++ b/packages/aml-backoffice-ui/src/hooks/useOfficer.ts
@@ -3,10 +3,15 @@ import {
Codec,
LockedAccount,
OfficerAccount,
+ OfficerId,
+ SigningKey,
buildCodecForObject,
codecForAbsoluteTime,
codecForString,
+ codecOptional,
createNewOfficerAccount,
+ decodeCrock,
+ encodeCrock,
unlockOfficerAccount,
} from "@gnu-taler/taler-util";
import {
@@ -14,6 +19,7 @@ import {
useLocalStorage,
useMemoryStorage,
} from "@gnu-taler/web-util/browser";
+import { useMemo } from "preact/hooks";
export interface Officer {
account: LockedAccount;
@@ -22,6 +28,17 @@ export interface Officer {
const codecForLockedAccount = codecForString() as Codec<LockedAccount>;
+type OfficerAccountString = {
+ id: string,
+ strKey: string;
+}
+
+export const codecForOfficerAccount = (): Codec<OfficerAccountString> =>
+ buildCodecForObject<OfficerAccountString>()
+ .property("id", codecForString()) // FIXME
+ .property("strKey", codecForString()) // FIXME
+ .build("OfficerAccount");
+
export const codecForOfficer = (): Codec<Officer> =>
buildCodecForObject<Officer>()
.property("account", codecForLockedAccount) // FIXME
@@ -47,26 +64,41 @@ interface OfficerReady {
}
const OFFICER_KEY = buildStorageKey("officer", codecForOfficer());
+const DEV_ACCOUNT_KEY = buildStorageKey("account-dev", codecForOfficerAccount());
const ACCOUNT_KEY = "account";
export function useOfficer(): OfficerState {
- const accountStorage = useMemoryStorage<OfficerAccount>(ACCOUNT_KEY);
- const officerStorage = useLocalStorage(OFFICER_KEY);
+ // dev account, is save when reloaded.
+ const accountStorage = useLocalStorage(DEV_ACCOUNT_KEY);
+ const account = useMemo(() => {
+ if (!accountStorage.value) return undefined
+ return {
+ id: accountStorage.value.id as OfficerId,
+ signingKey: decodeCrock(accountStorage.value.strKey) as SigningKey
+ }
+ }, [accountStorage.value])
+
+
+ // const accountStorage = useMemoryStorage<OfficerAccount>(ACCOUNT_KEY);
+ // const account = accountStorage.value;
+ const officerStorage = useLocalStorage(OFFICER_KEY);
const officer = officerStorage.value;
- const account = accountStorage.value;
+
if (officer === undefined) {
return {
state: "not-found",
create: async (pwd: string) => {
- const { id, safe, signingKey } = await createNewOfficerAccount(pwd);
+ const { id, safe, signingKey } = await createNewOfficerAccount(pwd);
officerStorage.update({
account: safe,
when: AbsoluteTime.now(),
});
- accountStorage.update({ id, signingKey });
+ // accountStorage.update({ id, signingKey });
+ const strKey = encodeCrock(signingKey)
+ accountStorage.update({id, strKey })
},
};
}
@@ -79,14 +111,15 @@ export function useOfficer(): OfficerState {
},
tryUnlock: async (pwd: string) => {
const ac = await unlockOfficerAccount(officer.account, pwd);
- accountStorage.update(ac);
+ // accountStorage.update(ac);
+ accountStorage.update({id: ac.id, strKey: encodeCrock(ac.signingKey)})
},
};
}
return {
state: "ready",
- account: account,
+ account,
lock: () => {
accountStorage.reset();
},
diff --git a/packages/aml-backoffice-ui/src/hooks/useSettings.ts b/packages/aml-backoffice-ui/src/hooks/useSettings.ts
index 52f6f1614..f1610576e 100644
--- a/packages/aml-backoffice-ui/src/hooks/useSettings.ts
+++ b/packages/aml-backoffice-ui/src/hooks/useSettings.ts
@@ -27,25 +27,29 @@ import { buildStorageKey, useLocalStorage, useTranslationContext } from "@gnu-ta
interface Settings {
allowInsecurePassword: boolean;
+ keepSessionAfterReload: boolean;
}
export function getAllBooleanSettings(): Array<keyof Settings> {
- return ["allowInsecurePassword"]
+ return ["allowInsecurePassword", "keepSessionAfterReload"]
}
export function getLabelForSetting(k: keyof Settings, i18n: ReturnType<typeof useTranslationContext>["i18n"]): TranslatedString {
switch (k) {
case "allowInsecurePassword": return i18n.str`Allow Insecure password`
+ case "keepSessionAfterReload": return i18n.str`Keep session after reload`
}
}
export const codecForSettings = (): Codec<Settings> =>
buildCodecForObject<Settings>()
- .property("allowInsecurePassword", (codecForBoolean()))
- .build("Settings");
+ .property("allowInsecurePassword", (codecForBoolean()))
+ .property("keepSessionAfterReload", (codecForBoolean()))
+ .build("Settings");
const defaultSettings: Settings = {
allowInsecurePassword: false,
+ keepSessionAfterReload: false,
};
const EXCHANGE_SETTINGS_KEY = buildStorageKey(