keycloak_theme/src/login/pages/LoginUpdatePassword.tsx

174 lines
7.5 KiB
TypeScript
Raw Normal View History

2024-12-09 05:06:47 +01:00
import type { JSX } from "keycloakify/tools/JSX";
2024-05-11 17:37:33 +02:00
import { useEffect, useReducer } from "react";
2024-09-22 20:41:18 +02:00
import { kcSanitize } from "keycloakify/lib/kcSanitize";
import { assert } from "keycloakify/tools/assert";
2024-06-09 08:27:07 +02:00
import { getKcClsx, type KcClsx } from "keycloakify/login/lib/kcClsx";
import type { PageProps } from "keycloakify/login/pages/PageProps";
import type { KcContext } from "../KcContext";
import type { I18n } from "../i18n";
2021-12-28 00:08:25 +03:00
export default function LoginUpdatePassword(props: PageProps<Extract<KcContext, { pageId: "login-update-password.ftl" }>, I18n>) {
const { kcContext, i18n, doUseDefaultCss, Template, classes } = props;
2023-03-18 06:14:05 +01:00
2024-06-09 08:27:07 +02:00
const { kcClsx } = getKcClsx({
2023-03-21 05:27:31 +01:00
doUseDefaultCss,
2023-03-18 06:14:05 +01:00
classes
});
2021-12-28 00:08:25 +03:00
const { msg, msgStr } = i18n;
2021-12-28 00:08:25 +03:00
2024-05-11 17:37:33 +02:00
const { url, messagesPerField, isAppInitiatedAction } = kcContext;
return (
2024-05-11 17:37:33 +02:00
<Template
2024-06-09 08:27:07 +02:00
kcContext={kcContext}
i18n={i18n}
2024-06-09 08:27:07 +02:00
doUseDefaultCss={doUseDefaultCss}
classes={classes}
2024-05-11 17:37:33 +02:00
displayMessage={!messagesPerField.existsError("password", "password-confirm")}
headerNode={msg("updatePasswordTitle")}
>
2024-06-09 08:27:07 +02:00
<form id="kc-passwd-update-form" className={kcClsx("kcFormClass")} action={url.loginAction} method="post">
<div className={kcClsx("kcFormGroupClass")}>
<div className={kcClsx("kcLabelWrapperClass")}>
<label htmlFor="password-new" className={kcClsx("kcLabelClass")}>
2023-03-21 02:36:13 +01:00
{msg("passwordNew")}
</label>
2024-07-27 01:13:47 +02:00
</div>
<div className={kcClsx("kcInputWrapperClass")}>
<PasswordWrapper kcClsx={kcClsx} i18n={i18n} passwordInputId="password-new">
<input
type="password"
id="password-new"
name="password-new"
className={kcClsx("kcInputClass")}
autoFocus
autoComplete="new-password"
aria-invalid={messagesPerField.existsError("password", "password-confirm")}
/>
</PasswordWrapper>
2024-05-11 17:37:33 +02:00
2024-07-27 01:13:47 +02:00
{messagesPerField.existsError("password") && (
<span
id="input-error-password"
className={kcClsx("kcInputErrorMessageClass")}
aria-live="polite"
dangerouslySetInnerHTML={{
2024-09-22 20:41:18 +02:00
__html: kcSanitize(messagesPerField.get("password"))
2024-07-27 01:13:47 +02:00
}}
/>
)}
</div>
2023-03-21 02:36:13 +01:00
</div>
2021-12-28 00:08:25 +03:00
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcFormGroupClass")}>
<div className={kcClsx("kcLabelWrapperClass")}>
<label htmlFor="password-confirm" className={kcClsx("kcLabelClass")}>
2023-03-21 02:36:13 +01:00
{msg("passwordConfirm")}
</label>
</div>
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcInputWrapperClass")}>
<PasswordWrapper kcClsx={kcClsx} i18n={i18n} passwordInputId="password-confirm">
2024-05-11 17:37:33 +02:00
<input
type="password"
id="password-confirm"
name="password-confirm"
2024-06-09 08:27:07 +02:00
className={kcClsx("kcInputClass")}
2024-05-11 17:37:33 +02:00
autoFocus
autoComplete="new-password"
aria-invalid={messagesPerField.existsError("password", "password-confirm")}
/>
</PasswordWrapper>
2021-12-28 00:08:25 +03:00
2024-05-11 17:37:33 +02:00
{messagesPerField.existsError("password-confirm") && (
2024-07-04 19:53:57 +02:00
<span
id="input-error-password-confirm"
className={kcClsx("kcInputErrorMessageClass")}
aria-live="polite"
dangerouslySetInnerHTML={{
2024-09-22 20:41:18 +02:00
__html: kcSanitize(messagesPerField.get("password-confirm"))
2024-07-04 19:53:57 +02:00
}}
/>
2024-05-11 17:37:33 +02:00
)}
2023-03-21 02:36:13 +01:00
</div>
2024-07-27 01:13:47 +02:00
</div>
<div className={kcClsx("kcFormGroupClass")}>
<LogoutOtherSessions kcClsx={kcClsx} i18n={i18n} />
<div id="kc-form-buttons" className={kcClsx("kcFormButtonsClass")}>
<input
className={kcClsx(
"kcButtonClass",
"kcButtonPrimaryClass",
!isAppInitiatedAction && "kcButtonBlockClass",
"kcButtonLargeClass"
2024-05-11 17:37:33 +02:00
)}
2024-07-27 01:13:47 +02:00
type="submit"
value={msgStr("doSubmit")}
/>
{isAppInitiatedAction && (
<button
className={kcClsx("kcButtonClass", "kcButtonDefaultClass", "kcButtonLargeClass")}
type="submit"
name="cancel-aia"
value="true"
>
{msg("doCancel")}
</button>
)}
</div>
2023-03-21 02:36:13 +01:00
</div>
</form>
</Template>
);
}
2024-05-11 17:37:33 +02:00
2024-06-09 08:27:07 +02:00
function LogoutOtherSessions(props: { kcClsx: KcClsx; i18n: I18n }) {
const { kcClsx, i18n } = props;
2024-05-11 17:37:33 +02:00
const { msg } = i18n;
return (
2024-06-09 08:27:07 +02:00
<div id="kc-form-options" className={kcClsx("kcFormOptionsClass")}>
<div className={kcClsx("kcFormOptionsWrapperClass")}>
2024-05-11 17:37:33 +02:00
<div className="checkbox">
<label>
<input type="checkbox" id="logout-sessions" name="logout-sessions" value="on" defaultChecked={true} />
{msg("logoutOtherSessions")}
</label>
</div>
</div>
</div>
);
}
2024-06-09 08:27:07 +02:00
function PasswordWrapper(props: { kcClsx: KcClsx; i18n: I18n; passwordInputId: string; children: JSX.Element }) {
const { kcClsx, i18n, passwordInputId, children } = props;
2024-05-11 17:37:33 +02:00
const { msgStr } = i18n;
const [isPasswordRevealed, toggleIsPasswordRevealed] = useReducer((isPasswordRevealed: boolean) => !isPasswordRevealed, false);
useEffect(() => {
const passwordInputElement = document.getElementById(passwordInputId);
assert(passwordInputElement instanceof HTMLInputElement);
passwordInputElement.type = isPasswordRevealed ? "text" : "password";
}, [isPasswordRevealed]);
return (
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcInputGroup")}>
2024-05-11 17:37:33 +02:00
{children}
<button
type="button"
2024-06-09 08:27:07 +02:00
className={kcClsx("kcFormPasswordVisibilityButtonClass")}
2024-05-11 17:37:33 +02:00
aria-label={msgStr(isPasswordRevealed ? "hidePassword" : "showPassword")}
aria-controls={passwordInputId}
onClick={toggleIsPasswordRevealed}
>
2024-06-09 08:27:07 +02:00
<i className={kcClsx(isPasswordRevealed ? "kcFormPasswordVisibilityIconHide" : "kcFormPasswordVisibilityIconShow")} aria-hidden />
2024-05-11 17:37:33 +02:00
</button>
</div>
);
}