2022-10-04 00:35:04 -04:00
|
|
|
import React, { useState, memo } from "react";
|
2022-10-13 11:58:31 +02:00
|
|
|
import DefaultTemplate from "./Template";
|
|
|
|
import type { TemplateProps } from "./Template";
|
2022-10-04 00:35:04 -04:00
|
|
|
import type { KcProps } from "./KcProps";
|
|
|
|
import type { KcContextBase } from "../getKcContext/KcContextBase";
|
2022-10-16 00:49:49 +02:00
|
|
|
import { clsx } from "../tools/clsx";
|
2022-10-04 00:35:04 -04:00
|
|
|
import { useConstCallback } from "powerhooks/useConstCallback";
|
|
|
|
import type { FormEventHandler } from "react";
|
|
|
|
import type { I18n } from "../i18n";
|
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
export type LoginUsernameProps = KcProps & {
|
|
|
|
kcContext: KcContextBase.LoginUsername;
|
|
|
|
i18n: I18n;
|
|
|
|
doFetchDefaultThemeResources?: boolean;
|
|
|
|
Template?: (props: TemplateProps) => JSX.Element | null;
|
|
|
|
};
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
const LoginUsername = memo((props: LoginUsernameProps) => {
|
|
|
|
const { kcContext, i18n, doFetchDefaultThemeResources = true, Template = DefaultTemplate, ...kcProps } = props;
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
const { social, realm, url, usernameHidden, login, registrationDisabled } = kcContext;
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
const { msg, msgStr } = i18n;
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
const [isLoginButtonDisabled, setIsLoginButtonDisabled] = useState(false);
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
const onSubmit = useConstCallback<FormEventHandler<HTMLFormElement>>(e => {
|
|
|
|
e.preventDefault();
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
setIsLoginButtonDisabled(true);
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
const formElement = e.target as HTMLFormElement;
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
//NOTE: Even if we login with email Keycloak expect username and password in
|
|
|
|
//the POST request.
|
|
|
|
formElement.querySelector("input[name='email']")?.setAttribute("name", "username");
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
formElement.submit();
|
|
|
|
});
|
2022-10-04 00:35:04 -04:00
|
|
|
|
2022-10-13 11:58:31 +02:00
|
|
|
return (
|
|
|
|
<Template
|
|
|
|
{...{ kcContext, i18n, doFetchDefaultThemeResources, ...kcProps }}
|
|
|
|
displayInfo={social.displayInfo}
|
|
|
|
displayWide={realm.password && social.providers !== undefined}
|
|
|
|
headerNode={msg("doLogIn")}
|
|
|
|
formNode={
|
2022-10-16 00:49:49 +02:00
|
|
|
<div id="kc-form" className={clsx(realm.password && social.providers !== undefined && kcProps.kcContentWrapperClass)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
<div
|
|
|
|
id="kc-form-wrapper"
|
2022-10-16 00:49:49 +02:00
|
|
|
className={clsx(
|
2022-10-13 11:58:31 +02:00
|
|
|
realm.password && social.providers && [kcProps.kcFormSocialAccountContentClass, kcProps.kcFormSocialAccountClass]
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{realm.password && (
|
|
|
|
<form id="kc-form-login" onSubmit={onSubmit} action={url.loginAction} method="post">
|
2022-10-16 00:49:49 +02:00
|
|
|
<div className={clsx(kcProps.kcFormGroupClass)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
{!usernameHidden &&
|
|
|
|
(() => {
|
|
|
|
const label = !realm.loginWithEmailAllowed
|
|
|
|
? "username"
|
|
|
|
: realm.registrationEmailAsUsername
|
|
|
|
? "email"
|
|
|
|
: "usernameOrEmail";
|
|
|
|
|
|
|
|
const autoCompleteHelper: typeof label = label === "usernameOrEmail" ? "username" : label;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-10-16 00:49:49 +02:00
|
|
|
<label htmlFor={autoCompleteHelper} className={clsx(kcProps.kcLabelClass)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
{msg(label)}
|
2022-10-04 00:35:04 -04:00
|
|
|
</label>
|
2022-10-13 11:58:31 +02:00
|
|
|
<input
|
|
|
|
tabIndex={1}
|
|
|
|
id={autoCompleteHelper}
|
2022-10-16 00:49:49 +02:00
|
|
|
className={clsx(kcProps.kcInputClass)}
|
2022-10-13 11:58:31 +02:00
|
|
|
//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>
|
2022-10-16 00:49:49 +02:00
|
|
|
<div className={clsx(kcProps.kcFormGroupClass, kcProps.kcFormSettingClass)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
<div id="kc-form-options">
|
|
|
|
{realm.rememberMe && !usernameHidden && (
|
|
|
|
<div className="checkbox">
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
tabIndex={3}
|
|
|
|
id="rememberMe"
|
|
|
|
name="rememberMe"
|
|
|
|
type="checkbox"
|
|
|
|
{...(login.rememberMe
|
|
|
|
? {
|
|
|
|
"checked": true
|
|
|
|
}
|
|
|
|
: {})}
|
|
|
|
/>
|
|
|
|
{msg("rememberMe")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-10-04 00:35:04 -04:00
|
|
|
</div>
|
2022-10-13 11:58:31 +02:00
|
|
|
</div>
|
2022-10-16 00:49:49 +02:00
|
|
|
<div id="kc-form-buttons" className={clsx(kcProps.kcFormGroupClass)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
<input
|
|
|
|
tabIndex={4}
|
2022-10-16 00:49:49 +02:00
|
|
|
className={clsx(
|
2022-10-13 11:58:31 +02:00
|
|
|
kcProps.kcButtonClass,
|
|
|
|
kcProps.kcButtonPrimaryClass,
|
|
|
|
kcProps.kcButtonBlockClass,
|
|
|
|
kcProps.kcButtonLargeClass
|
|
|
|
)}
|
|
|
|
name="login"
|
|
|
|
id="kc-login"
|
|
|
|
type="submit"
|
|
|
|
value={msgStr("doLogIn")}
|
|
|
|
disabled={isLoginButtonDisabled}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</form>
|
2022-10-04 00:35:04 -04:00
|
|
|
)}
|
|
|
|
</div>
|
2022-10-13 11:58:31 +02:00
|
|
|
{realm.password && social.providers !== undefined && (
|
2022-10-16 00:49:49 +02:00
|
|
|
<div id="kc-social-providers" className={clsx(kcProps.kcFormSocialAccountContentClass, kcProps.kcFormSocialAccountClass)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
<ul
|
2022-10-16 00:49:49 +02:00
|
|
|
className={clsx(
|
2022-10-13 11:58:31 +02:00
|
|
|
kcProps.kcFormSocialAccountListClass,
|
|
|
|
social.providers.length > 4 && kcProps.kcFormSocialAccountDoubleListClass
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{social.providers.map(p => (
|
2022-10-16 00:49:49 +02:00
|
|
|
<li key={p.providerId} className={clsx(kcProps.kcFormSocialAccountListLinkClass)}>
|
|
|
|
<a href={p.loginUrl} id={`zocial-${p.alias}`} className={clsx("zocial", p.providerId)}>
|
2022-10-13 11:58:31 +02:00
|
|
|
<span>{p.displayName}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
2022-10-04 00:35:04 -04:00
|
|
|
</div>
|
2022-10-13 11:58:31 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
infoNode={
|
|
|
|
realm.password &&
|
|
|
|
realm.registrationAllowed &&
|
|
|
|
!registrationDisabled && (
|
|
|
|
<div id="kc-registration">
|
|
|
|
<span>
|
|
|
|
{msg("noAccount")}
|
|
|
|
<a tabIndex={6} href={url.registrationUrl}>
|
|
|
|
{msg("doRegister")}
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
2022-10-04 00:35:04 -04:00
|
|
|
|
|
|
|
export default LoginUsername;
|