234 lines
12 KiB
TypeScript
Raw Normal View History

2024-05-07 20:46:02 +02:00
import { useState, useEffect, useReducer } from "react";
import { assert } from "tsafe/assert";
2023-03-18 06:14:05 +01:00
import { clsx } from "keycloakify/tools/clsx";
2023-03-21 05:27:31 +01:00
import type { PageProps } from "keycloakify/login/pages/PageProps";
import { useGetClassName } from "keycloakify/login/lib/useGetClassName";
2023-03-18 18:27:50 +01:00
import type { KcContext } from "../kcContext";
import type { I18n } from "../i18n";
2021-03-02 23:48:31 +01:00
2023-03-18 06:14:05 +01:00
export default function Login(props: PageProps<Extract<KcContext, { pageId: "login.ftl" }>, I18n>) {
const { kcContext, i18n, doUseDefaultCss, Template, classes } = props;
const { getClassName } = useGetClassName({
2023-03-21 05:27:31 +01:00
doUseDefaultCss,
2023-03-18 06:14:05 +01:00
classes
});
2021-08-20 17:03:50 +02:00
2024-04-13 04:46:13 +02:00
const { social, realm, url, usernameHidden, login, auth, registrationDisabled, messagesPerField } = kcContext;
2021-08-20 17:03:50 +02:00
const { msg, msgStr } = i18n;
2021-03-02 23:48:31 +01:00
const [isLoginButtonDisabled, setIsLoginButtonDisabled] = useState(false);
2022-03-30 16:20:14 +02:00
return (
<Template
2023-03-18 06:14:05 +01:00
{...{ kcContext, i18n, doUseDefaultCss, classes }}
2024-04-13 04:46:13 +02:00
displayMessage={!messagesPerField.existsError("username", "password")}
headerNode={msg("loginAccountTitle")}
2024-05-10 18:30:48 +02:00
displayInfo={realm.password && realm.registrationAllowed && !registrationDisabled}
infoNode={
2024-05-10 18:30:48 +02:00
<div id="kc-registration-container">
<div id="kc-registration">
<span>
{msg("noAccount")}{" "}
<a tabIndex={8} href={url.registrationUrl}>
{msg("doRegister")}
</a>
</span>
</div>
</div>
}
2024-04-13 04:46:13 +02:00
socialProvidersNode={
2024-05-07 20:46:02 +02:00
<>
{realm.password && social.providers?.length && (
<div id="kc-social-providers" className={getClassName("kcFormSocialAccountSectionClass")}>
<hr />
<h2>{msg("identity-provider-login-label")}</h2>
<ul
className={clsx(
getClassName("kcFormSocialAccountListClass"),
social.providers.length > 3 && getClassName("kcFormSocialAccountListGridClass")
)}
>
{social.providers.map((...[p, , providers]) => (
<li key={p.alias}>
<a
id={`social-${p.alias}`}
className={clsx(
getClassName("kcFormSocialAccountListButtonClass"),
providers.length > 3 && getClassName("kcFormSocialAccountGridItem")
)}
type="button"
href={p.loginUrl}
>
{p.iconClasses && (
<i className={clsx(getClassName("kcCommonLogoIdP"), p.iconClasses)} aria-hidden="true"></i>
)}
<span
className={clsx(getClassName("kcFormSocialAccountNameClass"), p.iconClasses && "kc-social-icon-text")}
>
{p.displayName}
</span>
</a>
</li>
))}
</ul>
</div>
)}
</>
2024-04-13 04:46:13 +02:00
}
2023-03-21 02:36:13 +01:00
>
2024-04-13 04:46:13 +02:00
<div id="kc-form">
<div id="kc-form-wrapper">
2023-03-21 02:36:13 +01:00
{realm.password && (
2024-04-13 04:46:13 +02:00
<form
id="kc-form-login"
onSubmit={() => {
setIsLoginButtonDisabled(true);
return true;
}}
action={url.loginAction}
method="post"
>
{!usernameHidden && (
<div className={getClassName("kcFormGroupClass")}>
<label htmlFor="username" className={getClassName("kcLabelClass")}>
{!realm.loginWithEmailAllowed
? msg("username")
: !realm.registrationEmailAsUsername
2024-05-20 19:30:15 +02:00
? msg("usernameOrEmail")
: msg("email")}
2024-04-13 04:46:13 +02:00
</label>
<input
tabIndex={2}
id="username"
className={getClassName("kcInputClass")}
name="username"
defaultValue={login.username ?? ""}
2024-04-13 04:46:13 +02:00
type="text"
autoFocus
autoComplete="username"
2024-05-07 20:46:02 +02:00
aria-invalid={messagesPerField.existsError("username", "password")}
2024-04-13 04:46:13 +02:00
/>
{messagesPerField.existsError("username", "password") && (
<span id="input-error" className={getClassName("kcInputErrorMessageClass")} aria-live="polite">
{messagesPerField.getFirstError("username", "password")}
</span>
)}
</div>
)}
2023-03-21 02:36:13 +01:00
<div className={getClassName("kcFormGroupClass")}>
<label htmlFor="password" className={getClassName("kcLabelClass")}>
{msg("password")}
</label>
2024-05-07 20:46:02 +02:00
<PasswordWrapper getClassName={getClassName} i18n={i18n} passwordInputId="password">
2024-04-13 04:46:13 +02:00
<input
tabIndex={3}
id="password"
className={getClassName("kcInputClass")}
name="password"
type="password"
autoComplete="current-password"
2024-05-07 20:46:02 +02:00
aria-invalid={messagesPerField.existsError("username", "password")}
2024-04-13 04:46:13 +02:00
/>
2024-05-07 20:46:02 +02:00
</PasswordWrapper>
2024-04-13 04:46:13 +02:00
{usernameHidden && messagesPerField.existsError("username", "password") && (
<span id="input-error" className={getClassName("kcInputErrorMessageClass")} aria-live="polite">
{messagesPerField.getFirstError("username", "password")}
</span>
)}
2023-03-21 02:36:13 +01:00
</div>
2024-05-07 20:46:02 +02:00
2023-03-21 02:36:13 +01:00
<div className={clsx(getClassName("kcFormGroupClass"), getClassName("kcFormSettingClass"))}>
<div id="kc-form-options">
{realm.rememberMe && !usernameHidden && (
2023-03-21 02:36:13 +01:00
<div className="checkbox">
<label>
2024-05-07 20:46:02 +02:00
<input
tabIndex={5}
id="rememberMe"
name="rememberMe"
type="checkbox"
defaultChecked={!!login.rememberMe}
/>{" "}
2023-03-21 02:36:13 +01:00
{msg("rememberMe")}
</label>
</div>
)}
</div>
<div className={getClassName("kcFormOptionsWrapperClass")}>
{realm.resetPasswordAllowed && (
<span>
2024-04-13 04:46:13 +02:00
<a tabIndex={6} href={url.loginResetCredentialsUrl}>
2023-03-21 02:36:13 +01:00
{msg("doForgotPassword")}
</a>
</span>
)}
</div>
</div>
2024-05-07 20:46:02 +02:00
2023-03-21 02:36:13 +01:00
<div id="kc-form-buttons" className={getClassName("kcFormGroupClass")}>
2024-05-07 20:46:02 +02:00
<input type="hidden" id="id-hidden-input" name="credentialId" value={auth.selectedCredential} />
2023-03-21 02:36:13 +01:00
<input
2024-04-13 04:46:13 +02:00
tabIndex={7}
2024-05-07 20:46:02 +02:00
disabled={isLoginButtonDisabled}
2023-03-21 02:36:13 +01:00
className={clsx(
getClassName("kcButtonClass"),
getClassName("kcButtonPrimaryClass"),
getClassName("kcButtonBlockClass"),
getClassName("kcButtonLargeClass")
)}
name="login"
id="kc-login"
type="submit"
value={msgStr("doLogIn")}
/>
</div>
</form>
)}
</div>
</div>
</Template>
);
}
2024-05-07 20:46:02 +02:00
2024-05-11 01:30:23 +02:00
function PasswordWrapper(props: {
getClassName: ReturnType<typeof useGetClassName>["getClassName"];
i18n: I18n;
passwordInputId: string;
children: JSX.Element;
}) {
2024-05-07 20:46:02 +02:00
const { getClassName, i18n, passwordInputId, children } = props;
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 (
<div className={getClassName("kcInputGroup")}>
{children}
<button
type="button"
className={getClassName("kcFormPasswordVisibilityButtonClass")}
aria-label={msgStr(isPasswordRevealed ? "hidePassword" : "showPassword")}
aria-controls={passwordInputId}
onClick={toggleIsPasswordRevealed}
>
<i
className={getClassName(isPasswordRevealed ? "kcFormPasswordVisibilityIconHide" : "kcFormPasswordVisibilityIconShow")}
aria-hidden
/>
</button>
</div>
);
}