Files
element-call/src/auth/LoginPage.tsx

134 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-08-20 16:23:12 -07:00
/*
2022-05-27 10:00:14 -04:00
Copyright 2021-2022 New Vector Ltd
2021-08-20 16:23:12 -07:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { FC, FormEvent, useCallback, useRef, useState } from "react";
2021-08-20 16:23:12 -07:00
import { useHistory, useLocation, Link } from "react-router-dom";
2022-10-10 09:19:10 -04:00
import { Trans, useTranslation } from "react-i18next";
2022-05-27 10:00:14 -04:00
2022-01-05 16:34:01 -08:00
import { ReactComponent as Logo } from "../icons/LogoLarge.svg";
import { useClient } from "../ClientContext";
2022-01-05 17:27:01 -08:00
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
2022-01-05 16:34:01 -08:00
import { Button } from "../button";
import styles from "./LoginPage.module.css";
2022-01-05 16:47:53 -08:00
import { useInteractiveLogin } from "./useInteractiveLogin";
2022-02-02 15:02:40 -08:00
import { usePageTitle } from "../usePageTitle";
import { PosthogAnalytics } from "../analytics/PosthogAnalytics";
2022-12-20 17:26:45 +00:00
import { Config } from "../config/Config";
2021-08-20 16:23:12 -07:00
2022-05-27 10:00:14 -04:00
export const LoginPage: FC = () => {
2022-10-10 09:19:10 -04:00
const { t } = useTranslation();
usePageTitle(t("Login"));
2022-02-02 15:02:40 -08:00
const { setClient } = useClient();
2022-05-27 13:29:46 -04:00
const login = useInteractiveLogin();
2022-12-20 17:26:45 +00:00
const homeserver = Config.defaultHomeserverUrl(); // TODO: Make this configurable
2022-05-27 10:00:14 -04:00
const usernameRef = useRef<HTMLInputElement>();
const passwordRef = useRef<HTMLInputElement>();
2021-08-20 16:23:12 -07:00
const history = useHistory();
const location = useLocation();
const [loading, setLoading] = useState(false);
2022-05-27 10:00:14 -04:00
const [error, setError] = useState<Error>();
2021-08-20 16:23:12 -07:00
2021-12-09 12:58:30 -08:00
// TODO: Handle hitting login page with authenticated client
2021-08-20 16:23:12 -07:00
const onSubmitLoginForm = useCallback(
2022-05-27 10:00:14 -04:00
(e: FormEvent<HTMLFormElement>) => {
2021-08-20 16:23:12 -07:00
e.preventDefault();
setLoading(true);
2021-12-09 12:58:30 -08:00
login(homeserver, usernameRef.current.value, passwordRef.current.value)
.then(([client, session]) => {
setClient(client, session);
2021-08-20 16:23:12 -07:00
if (location.state && location.state.from) {
2021-12-09 12:58:30 -08:00
history.push(location.state.from);
2021-08-20 16:23:12 -07:00
} else {
2021-12-09 12:58:30 -08:00
history.push("/");
2021-08-20 16:23:12 -07:00
}
PosthogAnalytics.instance.eventLogin.track();
2021-09-10 12:20:17 -07:00
})
.catch((error) => {
setError(error);
setLoading(false);
});
2021-08-20 16:23:12 -07:00
},
2022-05-27 10:00:14 -04:00
[login, location, history, homeserver, setClient]
2021-08-20 16:23:12 -07:00
);
return (
<>
<div className={styles.container}>
<div className={styles.content}>
<div className={styles.formContainer}>
<Logo width="auto" height="auto" className={styles.logo} />
<h2>Log In</h2>
<h4>To continue to Element</h4>
<form onSubmit={onSubmitLoginForm}>
<FieldRow>
<InputField
type="text"
ref={usernameRef}
2022-10-10 09:19:10 -04:00
placeholder={t("Username")}
label={t("Username")}
autoCorrect="off"
autoCapitalize="none"
2021-12-15 10:54:01 -08:00
prefix="@"
suffix={`:${Config.defaultServerName()}`}
data-testid="login_username"
/>
</FieldRow>
<FieldRow>
<InputField
type="password"
ref={passwordRef}
2022-10-10 09:19:10 -04:00
placeholder={t("Password")}
label={t("Password")}
data-testid="login_password"
/>
</FieldRow>
{error && (
<FieldRow>
2022-10-10 09:19:10 -04:00
<ErrorMessage error={error} />
2021-08-20 16:23:12 -07:00
</FieldRow>
)}
<FieldRow>
<Button
type="submit"
disabled={loading}
data-testid="login_login"
>
2022-10-10 09:19:10 -04:00
{loading ? t("Logging in…") : t("Login")}
</Button>
</FieldRow>
</form>
</div>
<div className={styles.authLinks}>
<p>Not registered yet?</p>
<p>
2022-10-10 09:19:10 -04:00
<Trans>
<Link to="/register">Create an account</Link>
{" Or "}
<Link to="/">Access as a guest</Link>
</Trans>
</p>
</div>
</div>
</div>
2021-08-20 16:23:12 -07:00
</>
);
2022-05-27 10:00:14 -04:00
};