diff options
author | Sebastian <sebasjm@gmail.com> | 2024-03-22 13:56:16 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-03-26 16:57:58 -0300 |
commit | e2bfbced7ab027c901913e83ff7dd82240661990 (patch) | |
tree | 33752605ccaf19498f8c2a64e0117db16f22ce26 /packages/merchant-backoffice-ui/src/paths/login/index.tsx | |
parent | 0c265558c4b7b78a13272abf1c4c84e3cf93c987 (diff) | |
download | wallet-core-e2bfbced7ab027c901913e83ff7dd82240661990.tar.gz wallet-core-e2bfbced7ab027c901913e83ff7dd82240661990.tar.bz2 wallet-core-e2bfbced7ab027c901913e83ff7dd82240661990.zip |
work in progress, new api being used. merchant now should move into using the full API
Diffstat (limited to 'packages/merchant-backoffice-ui/src/paths/login/index.tsx')
-rw-r--r-- | packages/merchant-backoffice-ui/src/paths/login/index.tsx | 82 |
1 files changed, 58 insertions, 24 deletions
diff --git a/packages/merchant-backoffice-ui/src/paths/login/index.tsx b/packages/merchant-backoffice-ui/src/paths/login/index.tsx index d94b7e506..1c0b915bd 100644 --- a/packages/merchant-backoffice-ui/src/paths/login/index.tsx +++ b/packages/merchant-backoffice-ui/src/paths/login/index.tsx @@ -19,7 +19,11 @@ * @author Sebastian Javier Marchano (sebasjm) */ -import { HttpStatusCode } from "@gnu-taler/taler-util"; +import { + AccessToken, + HttpStatusCode, + TalerAuthentication, +} from "@gnu-taler/taler-util"; import { useMerchantApiContext, useTranslationContext, @@ -27,40 +31,68 @@ import { import { ComponentChildren, Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { NotificationCard } from "../../components/menu/index.js"; -import { AccessToken } from "../../declaration.js"; -import { DEFAULT_ADMIN_USERNAME, useSessionState } from "../../hooks/session.js"; +import { + DEFAULT_ADMIN_USERNAME, + useSessionContext, +} from "../../context/session.js"; import { Notification } from "../../utils/types.js"; -interface Props { -} +interface Props {} -function normalizeToken(r: string): AccessToken { - return `secret-token:${r}` as AccessToken; -} +const tokenRequest = { + scope: "write", + duration: { + d_us: "forever" as const, + }, + refreshable: true, +}; export function LoginPage(_p: Props): VNode { const [token, setToken] = useState(""); const [notif, setNotif] = useState<Notification | undefined>(undefined); - const { state, logIn } = useSessionState(); + const { state, logIn } = useSessionContext(); const { lib } = useMerchantApiContext(); const { i18n } = useTranslationContext(); + async function doImpersonateImpl(instanceId: string) { + const result = await lib + .impersonate(instanceId) + .createAccessTokenMerchant(token, tokenRequest); + if (result.type === "ok") { + const { token } = result.body; + logIn({ token }); + return; + } else { + switch (result.case) { + case HttpStatusCode.Unauthorized: { + setNotif({ + message: "Your password is incorrect", + type: "ERROR", + }); + return; + } + case HttpStatusCode.NotFound: { + setNotif({ + message: "Your instance not found", + type: "ERROR", + }); + return; + } + } + } + } async function doLoginImpl() { - const secretToken = normalizeToken(token); - const result = await lib.authenticate.createAccessToken(secretToken, { - scope: "write", - duration: { - d_us: "forever" - }, - refreshable: true, - }); + const result = await lib.authenticate.createAccessTokenMerchant( + token, + tokenRequest, + ); if (result.type === "ok") { - const { access_token } = result.body; - logIn({ instance: state.instance, token: access_token }); + const { token } = result.body; + logIn({ token }); return; } else { - switch(result.case) { + switch (result.case) { case HttpStatusCode.Unauthorized: { setNotif({ message: "Your password is incorrect", @@ -79,8 +111,8 @@ export function LoginPage(_p: Props): VNode { } } - if (state.isAdmin && state.instance !== DEFAULT_ADMIN_USERNAME) { - //admin trying to access another instance + if (state.status === "loggedIn" && state.impersonate !== undefined) { + //the user is loggedin but trying to do an impersonation return ( <div class="columns is-centered" style={{ margin: "auto" }}> <div class="column is-two-thirds "> @@ -115,7 +147,9 @@ export function LoginPage(_p: Props): VNode { placeholder={"current access token"} name="token" onKeyPress={(e) => - e.keyCode === 13 ? doLoginImpl() : null + e.keyCode === 13 + ? doImpersonateImpl(state.instance) + : null } value={token} onInput={(e): void => setToken(e?.currentTarget.value)} @@ -133,7 +167,7 @@ export function LoginPage(_p: Props): VNode { borderTop: 0, }} > - <AsyncButton onClick={doLoginImpl}> + <AsyncButton onClick={() => doImpersonateImpl(state.instance)}> <i18n.Translate>Confirm</i18n.Translate> </AsyncButton> </footer> |