2022-07-30 00:42:07 +02:00
|
|
|
import React, { useState, memo } from "react";
|
2022-07-29 01:31:55 +02:00
|
|
|
import Template from "./Template";
|
2021-03-06 14:42:56 +01:00
|
|
|
import type { KcProps } from "./KcProps";
|
2021-06-23 08:16:51 +02:00
|
|
|
import type { KcContextBase } from "../getKcContext/KcContextBase";
|
2022-09-01 15:13:24 +02:00
|
|
|
import { useCssAndCx } from "../tools/useCssAndCx";
|
2021-07-21 22:10:28 +02:00
|
|
|
import { useConstCallback } from "powerhooks/useConstCallback";
|
2022-03-30 16:20:14 +02:00
|
|
|
import type { FormEventHandler } from "react";
|
2022-07-31 22:30:32 +02:00
|
|
|
import type { I18n } from "../i18n";
|
2021-03-02 23:48:31 +01:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const Login = memo(
|
|
|
|
({
|
|
|
|
kcContext,
|
|
|
|
i18n,
|
|
|
|
doFetchDefaultThemeResources = true,
|
|
|
|
...props
|
|
|
|
}: { kcContext: KcContextBase.Login; i18n: I18n; doFetchDefaultThemeResources?: boolean } & KcProps) => {
|
|
|
|
const { social, realm, url, usernameEditDisabled, login, auth, registrationDisabled } = kcContext;
|
2021-03-02 23:48:31 +01:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const { msg, msgStr } = i18n;
|
2021-08-20 17:03:50 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const { cx } = useCssAndCx();
|
2021-08-20 17:03:50 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const [isLoginButtonDisabled, setIsLoginButtonDisabled] = useState(false);
|
2021-03-02 23:48:31 +01:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const onSubmit = useConstCallback<FormEventHandler<HTMLFormElement>>(e => {
|
|
|
|
e.preventDefault();
|
2022-03-30 16:20:14 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
setIsLoginButtonDisabled(true);
|
2022-03-30 16:20:14 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const formElement = e.target as HTMLFormElement;
|
2022-03-30 16:20:14 +02:00
|
|
|
|
2022-09-27 21:30:33 +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-03-30 16:20:14 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
formElement.submit();
|
|
|
|
});
|
2021-03-02 23:48:31 +01:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
return (
|
|
|
|
<Template
|
|
|
|
{...{ kcContext, i18n, doFetchDefaultThemeResources, ...props }}
|
|
|
|
displayInfo={social.displayInfo}
|
|
|
|
displayWide={realm.password && social.providers !== undefined}
|
|
|
|
headerNode={msg("doLogIn")}
|
|
|
|
formNode={
|
|
|
|
<div id="kc-form" className={cx(realm.password && social.providers !== undefined && props.kcContentWrapperClass)}>
|
|
|
|
<div
|
|
|
|
id="kc-form-wrapper"
|
|
|
|
className={cx(
|
|
|
|
realm.password && social.providers && [props.kcFormSocialAccountContentClass, props.kcFormSocialAccountClass]
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{realm.password && (
|
|
|
|
<form id="kc-form-login" onSubmit={onSubmit} action={url.loginAction} method="post">
|
|
|
|
<div className={cx(props.kcFormGroupClass)}>
|
|
|
|
{(() => {
|
|
|
|
const label = !realm.loginWithEmailAllowed
|
|
|
|
? "username"
|
|
|
|
: realm.registrationEmailAsUsername
|
|
|
|
? "email"
|
|
|
|
: "usernameOrEmail";
|
2022-03-30 16:20:14 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
const autoCompleteHelper: typeof label = label === "usernameOrEmail" ? "username" : label;
|
2022-03-30 16:20:14 +02:00
|
|
|
|
2022-09-27 21:30:33 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<label htmlFor={autoCompleteHelper} className={cx(props.kcLabelClass)}>
|
|
|
|
{msg(label)}
|
|
|
|
</label>
|
2021-10-12 00:26:29 +02:00
|
|
|
<input
|
2022-09-27 21:30:33 +02:00
|
|
|
tabIndex={1}
|
|
|
|
id={autoCompleteHelper}
|
|
|
|
className={cx(props.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"
|
|
|
|
{...(usernameEditDisabled
|
|
|
|
? { "disabled": true }
|
|
|
|
: {
|
|
|
|
"autoFocus": true,
|
|
|
|
"autoComplete": "off"
|
|
|
|
})}
|
2021-10-12 00:26:29 +02:00
|
|
|
/>
|
2022-09-27 21:30:33 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
})()}
|
2021-03-02 23:48:31 +01:00
|
|
|
</div>
|
2022-09-27 21:30:33 +02:00
|
|
|
<div className={cx(props.kcFormGroupClass)}>
|
|
|
|
<label htmlFor="password" className={cx(props.kcLabelClass)}>
|
|
|
|
{msg("password")}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
tabIndex={2}
|
|
|
|
id="password"
|
|
|
|
className={cx(props.kcInputClass)}
|
|
|
|
name="password"
|
|
|
|
type="password"
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
2021-03-02 23:48:31 +01:00
|
|
|
</div>
|
2022-09-27 21:30:33 +02:00
|
|
|
<div className={cx(props.kcFormGroupClass, props.kcFormSettingClass)}>
|
|
|
|
<div id="kc-form-options">
|
|
|
|
{realm.rememberMe && !usernameEditDisabled && (
|
|
|
|
<div className="checkbox">
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
tabIndex={3}
|
|
|
|
id="rememberMe"
|
|
|
|
name="rememberMe"
|
|
|
|
type="checkbox"
|
|
|
|
{...(login.rememberMe
|
|
|
|
? {
|
|
|
|
"checked": true
|
|
|
|
}
|
|
|
|
: {})}
|
|
|
|
/>
|
|
|
|
{msg("rememberMe")}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className={cx(props.kcFormOptionsWrapperClass)}>
|
|
|
|
{realm.resetPasswordAllowed && (
|
|
|
|
<span>
|
|
|
|
<a tabIndex={5} href={url.loginResetCredentialsUrl}>
|
|
|
|
{msg("doForgotPassword")}
|
|
|
|
</a>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="kc-form-buttons" className={cx(props.kcFormGroupClass)}>
|
|
|
|
<input
|
|
|
|
type="hidden"
|
|
|
|
id="id-hidden-input"
|
|
|
|
name="credentialId"
|
|
|
|
{...(auth?.selectedCredential !== undefined
|
|
|
|
? {
|
|
|
|
"value": auth.selectedCredential
|
|
|
|
}
|
|
|
|
: {})}
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
tabIndex={4}
|
|
|
|
className={cx(
|
|
|
|
props.kcButtonClass,
|
|
|
|
props.kcButtonPrimaryClass,
|
|
|
|
props.kcButtonBlockClass,
|
|
|
|
props.kcButtonLargeClass
|
|
|
|
)}
|
|
|
|
name="login"
|
|
|
|
id="kc-login"
|
|
|
|
type="submit"
|
|
|
|
value={msgStr("doLogIn")}
|
|
|
|
disabled={isLoginButtonDisabled}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{realm.password && social.providers !== undefined && (
|
|
|
|
<div id="kc-social-providers" className={cx(props.kcFormSocialAccountContentClass, props.kcFormSocialAccountClass)}>
|
|
|
|
<ul
|
|
|
|
className={cx(
|
|
|
|
props.kcFormSocialAccountListClass,
|
|
|
|
social.providers.length > 4 && props.kcFormSocialAccountDoubleListClass
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{social.providers.map(p => (
|
|
|
|
<li key={p.providerId} className={cx(props.kcFormSocialAccountListLinkClass)}>
|
|
|
|
<a href={p.loginUrl} id={`zocial-${p.alias}`} className={cx("zocial", p.providerId)}>
|
|
|
|
<span>{p.displayName}</span>
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2021-10-12 00:26:29 +02:00
|
|
|
)}
|
|
|
|
</div>
|
2022-09-27 21:30:33 +02:00
|
|
|
}
|
|
|
|
infoNode={
|
|
|
|
realm.password &&
|
|
|
|
realm.registrationAllowed &&
|
|
|
|
!registrationDisabled && (
|
|
|
|
<div id="kc-registration">
|
|
|
|
<span>
|
|
|
|
{msg("noAccount")}
|
|
|
|
<a tabIndex={6} href={url.registrationUrl}>
|
|
|
|
{msg("doRegister")}
|
|
|
|
</a>
|
|
|
|
</span>
|
2021-10-11 21:35:40 +02:00
|
|
|
</div>
|
2022-09-27 21:30:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2022-07-29 01:31:55 +02:00
|
|
|
|
|
|
|
export default Login;
|