226 lines
11 KiB
TypeScript
Raw Normal View History

2024-05-07 20:46:02 +02:00
import { useState, useEffect, useReducer } from "react";
import { assert } from "keycloakify/tools/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";
2024-06-09 08:27:07 +02:00
import { getKcClsx, type KcClsx } from "keycloakify/login/lib/kcClsx";
import type { KcContext } from "../KcContext";
import type { I18n } from "../i18n";
2021-03-02 23:48:31 +01:00
export default function Login(props: PageProps<Extract<KcContext, { pageId: "login.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-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
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-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
<>
2024-08-30 15:35:21 +02:00
{realm.password && social.providers !== undefined && social.providers.length !== 0 && (
2024-06-09 08:27:07 +02:00
<div id="kc-social-providers" className={kcClsx("kcFormSocialAccountSectionClass")}>
2024-05-07 20:46:02 +02:00
<hr />
<h2>{msg("identity-provider-login-label")}</h2>
2024-06-09 08:27:07 +02:00
<ul className={kcClsx("kcFormSocialAccountListClass", social.providers.length > 3 && "kcFormSocialAccountListGridClass")}>
2024-05-07 20:46:02 +02:00
{social.providers.map((...[p, , providers]) => (
<li key={p.alias}>
<a
id={`social-${p.alias}`}
2024-06-09 08:27:07 +02:00
className={kcClsx(
"kcFormSocialAccountListButtonClass",
providers.length > 3 && "kcFormSocialAccountGridItem"
2024-05-07 20:46:02 +02:00
)}
type="button"
href={p.loginUrl}
>
2024-06-09 08:27:07 +02:00
{p.iconClasses && <i className={clsx(kcClsx("kcCommonLogoIdP"), p.iconClasses)} aria-hidden="true"></i>}
2024-07-04 19:53:57 +02:00
<span
className={clsx(kcClsx("kcFormSocialAccountNameClass"), p.iconClasses && "kc-social-icon-text")}
dangerouslySetInnerHTML={{ __html: p.displayName }}
></span>
2024-05-07 20:46:02 +02:00
</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 && (
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcFormGroupClass")}>
<label htmlFor="username" className={kcClsx("kcLabelClass")}>
2024-04-13 04:46:13 +02:00
{!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"
2024-06-09 08:27:07 +02:00
className={kcClsx("kcInputClass")}
2024-04-13 04:46:13 +02:00
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") && (
2024-07-04 19:53:57 +02:00
<span
id="input-error"
className={kcClsx("kcInputErrorMessageClass")}
aria-live="polite"
dangerouslySetInnerHTML={{
__html: messagesPerField.getFirstError("username", "password")
}}
/>
2024-04-13 04:46:13 +02:00
)}
</div>
)}
2023-03-21 02:36:13 +01:00
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcFormGroupClass")}>
<label htmlFor="password" className={kcClsx("kcLabelClass")}>
2023-03-21 02:36:13 +01:00
{msg("password")}
</label>
2024-06-09 08:27:07 +02:00
<PasswordWrapper kcClsx={kcClsx} i18n={i18n} passwordInputId="password">
2024-04-13 04:46:13 +02:00
<input
tabIndex={3}
id="password"
2024-06-09 08:27:07 +02:00
className={kcClsx("kcInputClass")}
2024-04-13 04:46:13 +02:00
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") && (
2024-07-04 19:53:57 +02:00
<span
id="input-error"
className={kcClsx("kcInputErrorMessageClass")}
aria-live="polite"
dangerouslySetInnerHTML={{
__html: messagesPerField.getFirstError("username", "password")
}}
/>
2024-04-13 04:46:13 +02:00
)}
2023-03-21 02:36:13 +01:00
</div>
2024-05-07 20:46:02 +02:00
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcFormGroupClass", "kcFormSettingClass")}>
2023-03-21 02:36:13 +01:00
<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>
2024-06-09 08:27:07 +02:00
<div className={kcClsx("kcFormOptionsWrapperClass")}>
2023-03-21 02:36:13 +01:00
{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
2024-06-09 08:27:07 +02:00
<div id="kc-form-buttons" className={kcClsx("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}
2024-06-09 08:27:07 +02:00
className={kcClsx("kcButtonClass", "kcButtonPrimaryClass", "kcButtonBlockClass", "kcButtonLargeClass")}
2023-03-21 02:36:13 +01:00
name="login"
id="kc-login"
type="submit"
value={msgStr("doLogIn")}
/>
</div>
</form>
)}
</div>
</div>
</Template>
);
}
2024-05-07 20:46:02 +02:00
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-07 20:46:02 +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-07 20:46:02 +02:00
{children}
<button
type="button"
2024-06-09 08:27:07 +02:00
className={kcClsx("kcFormPasswordVisibilityButtonClass")}
2024-05-07 20:46:02 +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-07 20:46:02 +02:00
</button>
</div>
);
}