import { notifyInfo, useLocalNotification, useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { ShowInputErrorLabel } from "@gnu-taler/web-util/browser"; import { useBankCoreApiContext } from "../../context/config.js"; import { useBackendState } from "../../hooks/backend.js"; import { undefinedIfEmpty, withRuntimeErrorHandling } from "../../utils.js"; import { doAutoFocus } from "../PaytoWireTransferForm.js"; import { ProfileNavigation } from "../ProfileNavigation.js"; import { assertUnreachable } from "../WithdrawalOperationPage.js"; import { LocalNotificationBanner } from "@gnu-taler/web-util/browser"; export function UpdateAccountPassword({ account: accountName, onCancel, onUpdateSuccess, focus, }: { onCancel: () => void; focus?: boolean, onUpdateSuccess: () => void; account: string; }): VNode { const { i18n } = useTranslationContext(); const { state: credentials } = useBackendState(); const token = credentials.status !== "loggedIn" ? undefined : credentials.token const { api } = useBankCoreApiContext(); const [current, setCurrent] = useState(); const [password, setPassword] = useState(); const [repeat, setRepeat] = useState(); 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`password doesn't match` : undefined, }); const [notification, notify, handleError] = useLocalNotification() async function doChangePassword() { if (!!errors || !password || !token) return; await handleError(async () => { const resp = await api.updatePassword({ username: accountName, token }, { old_password: current, new_password: password, }); if (resp.type === "ok") { notifyInfo(i18n.str`Password changed`); onUpdateSuccess(); } else { switch (resp.case) { case "unauthorized": return notify({ type: "error", title: i18n.str`Not authorized to change the password, maybe the session is invalid.` }) case "not-found": return notify({ type: "error", title: i18n.str`Account not found` }) case "user-require-old-password": return notify({ type: "error", title: i18n.str`Old password need to be provided in order to change new one. If you don't have it contact your account administrator.` }) case "wrong-old-password": return notify({ type: "error", title: i18n.str`Your current password doesn't match, can't change to a new password.` }) default: assertUnreachable(resp) } } }) } return ( {accountIsTheCurrentUser ? :

Account "{accountName}"

}

Update password

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

repeat the same password

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

your current password, for security

: undefined}
{onCancel ? :
}
); }