212 lines
11 KiB
TypeScript
Raw Normal View History

2024-04-13 04:46:13 +02:00
import { useState } from "react";
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")}
displayInfo={realm.password && realm.registrationAllowed && !registrationDisabled}
2024-04-13 04:46:13 +02:00
headerNode={msg("loginAccountTitle")}
infoNode={
2024-04-13 04:46:13 +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={
realm.password &&
social.providers && (
<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 => (
<li key={p.alias}>
<a
id={`social-${p.alias}`}
className={clsx(
getClassName("kcFormSocialAccountListButtonClass"),
social.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"), "kc-social-icon-text")}>
{p.displayName}
</span>
</>
) : (
<span className={getClassName("kcFormSocialAccountNameClass")}>{p.displayName}</span>
)}
</a>
</li>
))}
</ul>
</div>
)
}
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);
2023-03-21 02:36:13 +01:00
2024-04-13 04:46:13 +02:00
return true;
}}
action={url.loginAction}
method="post"
>
{!usernameHidden && (
<div className={getClassName("kcFormGroupClass")}>
<label htmlFor="username" className={getClassName("kcLabelClass")}>
{!realm.loginWithEmailAllowed
? msg("username")
: !realm.registrationEmailAsUsername
? msg("usernameOrEmail")
: msg("email")}
</label>
<input
tabIndex={2}
id="username"
className={getClassName("kcInputClass")}
name="username"
value={login.username ?? ""}
type="text"
autoFocus
autoComplete="username"
aria-invalid={messagesPerField.existsError("username", "password") ? true : undefined}
/>
{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-04-13 04:46:13 +02:00
<div className={getClassName("kcInputGroup")}>
<input
tabIndex={3}
id="password"
className={getClassName("kcInputClass")}
name="password"
type="password"
autoComplete="current-password"
aria-invalid={messagesPerField.existsError("username", "password") ? "true" : undefined}
/>
<button
className={getClassName("kcFormPasswordVisibilityButtonClass")}
type="button"
aria-label={msgStr("showPassword")}
aria-controls="password"
data-password-toggle
tabIndex={4}
data-icon-show={getClassName("kcFormPasswordVisibilityIconShow")}
data-icon-hide={getClassName("kcFormPasswordVisibilityIconHide")}
data-label-show={msg("showPassword")}
data-label-hide={msg("hidePassword")}
>
<i className={getClassName("kcFormPasswordVisibilityIconShow")} aria-hidden={true}></i>
</button>
</div>
{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>
<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-04-13 04:46:13 +02:00
{login.rememberMe ? (
<input tabIndex={5} id="rememberMe" name="rememberMe" type="checkbox" defaultChecked />
) : (
<input tabIndex={5} id="rememberMe" name="rememberMe" type="checkbox" />
)}
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>
<div id="kc-form-buttons" className={getClassName("kcFormGroupClass")}>
2024-04-13 04:46:13 +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}
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")}
disabled={isLoginButtonDisabled}
/>
</div>
</form>
)}
</div>
</div>
2024-04-13 04:46:13 +02:00
<script type="module" src={`${url.resourcesPath}/js/passwordVisibility.js`} />
2023-03-21 02:36:13 +01:00
</Template>
);
}