Update login-username.ftl page

This commit is contained in:
Joseph Garrone 2024-05-11 18:15:00 +02:00
parent d5376b80c2
commit 8683cf88fe
2 changed files with 94 additions and 110 deletions

View File

@ -307,15 +307,7 @@ export declare namespace KcContext {
rememberMe?: string; rememberMe?: string;
}; };
usernameHidden?: boolean; usernameHidden?: boolean;
social: { social: Login["social"];
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[];
};
}; };
export type LoginPassword = Common & { export type LoginPassword = Common & {

View File

@ -1,7 +1,5 @@
import type { FormEventHandler } from "react";
import { useState } from "react"; import { useState } from "react";
import { clsx } from "keycloakify/tools/clsx"; import { clsx } from "keycloakify/tools/clsx";
import { useConstCallback } from "keycloakify/tools/useConstCallback";
import type { PageProps } from "keycloakify/login/pages/PageProps"; import type { PageProps } from "keycloakify/login/pages/PageProps";
import { useGetClassName } from "keycloakify/login/lib/useGetClassName"; import { useGetClassName } from "keycloakify/login/lib/useGetClassName";
import type { KcContext } from "../kcContext"; import type { KcContext } from "../kcContext";
@ -15,90 +13,108 @@ export default function LoginUsername(props: PageProps<Extract<KcContext, { page
classes classes
}); });
const { social, realm, url, usernameHidden, login, registrationDisabled } = kcContext; const { social, realm, url, usernameHidden, login, registrationDisabled, messagesPerField } = kcContext;
const { msg, msgStr } = i18n; const { msg, msgStr } = i18n;
const [isLoginButtonDisabled, setIsLoginButtonDisabled] = useState(false); const [isLoginButtonDisabled, setIsLoginButtonDisabled] = useState(false);
const onSubmit = useConstCallback<FormEventHandler<HTMLFormElement>>(e => {
e.preventDefault();
setIsLoginButtonDisabled(true);
const formElement = e.target as HTMLFormElement;
//NOTE: Even if we login with email Keycloak expect username and password in
//the POST request.
formElement.querySelector("input[name='email']")?.setAttribute("name", "username");
formElement.submit();
});
return ( return (
<Template <Template
{...{ kcContext, i18n, doUseDefaultCss, classes }} {...{ kcContext, i18n, doUseDefaultCss, classes }}
displayInfo={social.displayInfo} displayMessage={!messagesPerField.existsError("username")}
displayWide={realm.password && social.providers !== undefined} displayInfo={realm.password && realm.registrationAllowed && !registrationDisabled}
headerNode={msg("doLogIn")}
infoNode={ infoNode={
realm.password && <div id="kc-registration">
realm.registrationAllowed && <span>
!registrationDisabled && ( {msg("noAccount")}
<div id="kc-registration"> <a tabIndex={6} href={url.registrationUrl}>
<span> {msg("doRegister")}
{msg("noAccount")} </a>
<a tabIndex={6} href={url.registrationUrl}> </span>
{msg("doRegister")} </div>
</a> }
</span> headerNode={msg("doLogIn")}
</div> socialProvidersNode={
) <>
{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>
)}
</>
} }
> >
<div id="kc-form" className={clsx(realm.password && social.providers !== undefined && getClassName("kcContentWrapperClass"))}> <div id="kc-form">
<div <div id="kc-form-wrapper">
id="kc-form-wrapper"
className={clsx(
realm.password &&
social.providers && [getClassName("kcFormSocialAccountContentClass"), getClassName("kcFormSocialAccountClass")]
)}
>
{realm.password && ( {realm.password && (
<form id="kc-form-login" onSubmit={onSubmit} action={url.loginAction} method="post"> <form
<div className={getClassName("kcFormGroupClass")}> id="kc-form-login"
{!usernameHidden && onSubmit={() => {
(() => { setIsLoginButtonDisabled(true);
const label = !realm.loginWithEmailAllowed return true;
? "username" }}
: realm.registrationEmailAsUsername action={url.loginAction}
? "email" method="post"
: "usernameOrEmail"; >
{!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="off"
aria-invalid={messagesPerField.existsError("username")}
/>
{messagesPerField.existsError("username") && (
<span id="input-error" className={getClassName("kcInputErrorMessageClass")} aria-live="polite">
{messagesPerField.getFirstError("username")}
</span>
)}
</div>
)}
const autoCompleteHelper: typeof label = label === "usernameOrEmail" ? "username" : label;
return (
<>
<label htmlFor={autoCompleteHelper} className={getClassName("kcLabelClass")}>
{msg(label)}
</label>
<input
tabIndex={1}
id={autoCompleteHelper}
className={getClassName("kcInputClass")}
//NOTE: This is used by Google Chrome auto fill so we use it to tell
//the browser how to pre fill the form but before submit we put it back
//to username because it is what keycloak expects.
name={autoCompleteHelper}
defaultValue={login.username ?? ""}
type="text"
autoFocus={true}
autoComplete="off"
/>
</>
);
})()}
</div>
<div className={clsx(getClassName("kcFormGroupClass"), getClassName("kcFormSettingClass"))}> <div className={clsx(getClassName("kcFormGroupClass"), getClassName("kcFormSettingClass"))}>
<div id="kc-form-options"> <div id="kc-form-options">
{realm.rememberMe && !usernameHidden && ( {realm.rememberMe && !usernameHidden && (
@ -109,21 +125,19 @@ export default function LoginUsername(props: PageProps<Extract<KcContext, { page
id="rememberMe" id="rememberMe"
name="rememberMe" name="rememberMe"
type="checkbox" type="checkbox"
{...(login.rememberMe === "on" defaultChecked={!!login.rememberMe}
? { />{" "}
"checked": true
}
: {})}
/>
{msg("rememberMe")} {msg("rememberMe")}
</label> </label>
</div> </div>
)} )}
</div> </div>
</div> </div>
<div id="kc-form-buttons" className={getClassName("kcFormGroupClass")}> <div id="kc-form-buttons" className={getClassName("kcFormGroupClass")}>
<input <input
tabIndex={4} tabIndex={4}
disabled={isLoginButtonDisabled}
className={clsx( className={clsx(
getClassName("kcButtonClass"), getClassName("kcButtonClass"),
getClassName("kcButtonPrimaryClass"), getClassName("kcButtonPrimaryClass"),
@ -134,33 +148,11 @@ export default function LoginUsername(props: PageProps<Extract<KcContext, { page
id="kc-login" id="kc-login"
type="submit" type="submit"
value={msgStr("doLogIn")} value={msgStr("doLogIn")}
disabled={isLoginButtonDisabled}
/> />
</div> </div>
</form> </form>
)} )}
</div> </div>
{realm.password && social.providers !== undefined && (
<div
id="kc-social-providers"
className={clsx(getClassName("kcFormSocialAccountContentClass"), getClassName("kcFormSocialAccountClass"))}
>
<ul
className={clsx(
getClassName("kcFormSocialAccountListClass"),
social.providers.length > 4 && getClassName("kcFormSocialAccountDoubleListClass")
)}
>
{social.providers.map(p => (
<li key={p.providerId} className={getClassName("kcFormSocialAccountListLinkClass")}>
<a href={p.loginUrl} id={`zocial-${p.alias}`} className={clsx("zocial", p.providerId)}>
<span>{p.displayName}</span>
</a>
</li>
))}
</ul>
</div>
)}
</div> </div>
</Template> </Template>
); );