/* This file is part of GNU Taler (C) 2022-2024 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Taler; see the file COPYING. If not, see */ import { AbsoluteTime, HttpStatusCode, TalerErrorCode, TranslatedString, assertUnreachable, } from "@gnu-taler/taler-util"; import { LocalNotificationBanner, ShowInputErrorLabel, notifyInfo, useLocalNotification, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { useBankCoreApiContext } from "@gnu-taler/web-util/browser"; import { useSessionState } from "../../hooks/session.js"; import { useBankState } from "../../hooks/bank-state.js"; import { RouteDefinition } from "@gnu-taler/web-util/browser"; import { undefinedIfEmpty } from "../../utils.js"; import { doAutoFocus } from "../PaytoWireTransferForm.js"; import { ProfileNavigation } from "../ProfileNavigation.js"; export function UpdateAccountPassword({ account: accountName, routeClose, onUpdateSuccess, onAuthorizationRequired, routeMyAccountCashout, routeMyAccountDelete, routeMyAccountDetails, routeMyAccountPassword, routeConversionConfig, focus, routeHere, }: { routeClose: RouteDefinition; routeHere: RouteDefinition<{ account: string }>; routeMyAccountDetails: RouteDefinition; routeMyAccountDelete: RouteDefinition; routeMyAccountPassword: RouteDefinition; routeMyAccountCashout: RouteDefinition; routeConversionConfig: RouteDefinition; focus?: boolean; onAuthorizationRequired: () => void; onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); const { state: credentials } = useSessionState(); const token = credentials.status !== "loggedIn" ? undefined : credentials.token; const { lib: { bank: api }, } = useBankCoreApiContext(); const [current, setCurrent] = useState(); const [password, setPassword] = useState(); const [repeat, setRepeat] = useState(); const [, updateBankState] = useBankState(); const accountIsTheCurrentUser = credentials.status === "loggedIn" ? credentials.username === accountName : false; const errors = undefinedIfEmpty({ current: !accountIsTheCurrentUser ? undefined : !current ? i18n.str`Required` : undefined, password: !password ? i18n.str`Required` : undefined, repeat: !repeat ? i18n.str`Required` : password !== repeat ? i18n.str`Repeated password doesn't match` : undefined, }); const [notification, notify, handleError] = useLocalNotification(); async function doChangePassword() { if (!!errors || !password || !token) return; await handleError(async () => { const request = { old_password: current, new_password: password, }; const resp = await api.updatePassword( { username: accountName, token }, request, ); if (resp.type === "ok") { notifyInfo(i18n.str`Password changed`); onUpdateSuccess(); } else { switch (resp.case) { case HttpStatusCode.Unauthorized: return notify({ type: "error", title: i18n.str`Not authorized to change the password, maybe the session is invalid.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, when: AbsoluteTime.now(), }); case HttpStatusCode.NotFound: return notify({ type: "error", title: i18n.str`Account not found`, description: resp.detail.hint as TranslatedString, debug: resp.detail, when: AbsoluteTime.now(), }); case TalerErrorCode.BANK_NON_ADMIN_PATCH_MISSING_OLD_PASSWORD: return notify({ type: "error", title: i18n.str`You need to provide the old password. If you don't have it contact your account administrator.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, when: AbsoluteTime.now(), }); case TalerErrorCode.BANK_PATCH_BAD_OLD_PASSWORD: return notify({ type: "error", title: i18n.str`Your current password doesn't match, can't change to a new password.`, description: resp.detail.hint as TranslatedString, debug: resp.detail, when: AbsoluteTime.now(), }); case HttpStatusCode.Accepted: { updateBankState("currentChallenge", { operation: "update-password", id: String(resp.body.challenge_id), location: routeHere.url({ account: accountName }), sent: AbsoluteTime.never(), request, }); return onAuthorizationRequired(); } default: assertUnreachable(resp); } } }); } return ( {accountIsTheCurrentUser ? ( ) : (

Account "{accountName}"

)}

Update password

{ e.preventDefault(); }} >
{accountIsTheCurrentUser ? (
{ setCurrent(e.currentTarget.value); }} autocomplete="off" />

Your current password, for security

) : undefined}
{ setPassword(e.currentTarget.value); }} autocomplete="off" />
{ setRepeat(e.currentTarget.value); }} // placeholder="" autocomplete="off" />

Repeat the same password

Cancel
); }