2021-08-20 16:23:12 -07:00
|
|
|
/*
|
|
|
|
Copyright 2021 New Vector Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-12-15 11:10:38 -08:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from "react";
|
2022-01-05 16:34:01 -08:00
|
|
|
import { useHistory, useLocation } from "react-router-dom";
|
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";
|
2022-01-05 17:19:03 -08:00
|
|
|
import { useClient } from "../ClientContext";
|
|
|
|
import { defaultHomeserverHost } from "../matrix-utils";
|
2022-01-05 16:47:53 -08:00
|
|
|
import { useInteractiveRegistration } from "./useInteractiveRegistration";
|
2021-12-10 12:46:18 -08:00
|
|
|
import styles from "./LoginPage.module.css";
|
2022-01-05 16:34:01 -08:00
|
|
|
import { ReactComponent as Logo } from "../icons/LogoLarge.svg";
|
|
|
|
import { LoadingView } from "../FullScreenView";
|
|
|
|
import { useRecaptcha } from "./useRecaptcha";
|
|
|
|
import { Caption, Link } from "../typography/Typography";
|
2021-08-20 16:23:12 -07:00
|
|
|
|
2021-12-09 12:58:30 -08:00
|
|
|
export function RegisterPage() {
|
2021-12-16 16:43:35 -08:00
|
|
|
const {
|
|
|
|
loading,
|
|
|
|
client,
|
|
|
|
changePassword,
|
|
|
|
isAuthenticated,
|
|
|
|
isPasswordlessUser,
|
|
|
|
} = useClient();
|
2021-12-15 11:10:38 -08:00
|
|
|
const confirmPasswordRef = useRef();
|
2021-08-20 16:23:12 -07:00
|
|
|
const history = useHistory();
|
|
|
|
const location = useLocation();
|
2021-12-16 16:43:35 -08:00
|
|
|
const [registering, setRegistering] = useState(false);
|
2021-09-10 12:20:17 -07:00
|
|
|
const [error, setError] = useState();
|
2021-12-15 11:10:38 -08:00
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [passwordConfirmation, setPasswordConfirmation] = useState("");
|
2021-12-20 15:56:39 -08:00
|
|
|
const [{ privacyPolicyUrl, recaptchaKey }, register] =
|
|
|
|
useInteractiveRegistration();
|
2022-01-05 16:34:01 -08:00
|
|
|
const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey);
|
2021-08-20 16:23:12 -07:00
|
|
|
|
|
|
|
const onSubmitRegisterForm = useCallback(
|
|
|
|
(e) => {
|
|
|
|
e.preventDefault();
|
2021-12-16 16:43:35 -08:00
|
|
|
const data = new FormData(e.target);
|
|
|
|
const userName = data.get("userName");
|
|
|
|
const password = data.get("password");
|
|
|
|
const passwordConfirmation = data.get("passwordConfirmation");
|
|
|
|
|
2022-01-05 16:34:01 -08:00
|
|
|
if (password !== passwordConfirmation) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-16 16:43:35 -08:00
|
|
|
|
2022-01-05 16:34:01 -08:00
|
|
|
async function submit() {
|
2021-12-20 15:56:39 -08:00
|
|
|
setRegistering(true);
|
2021-12-16 16:43:35 -08:00
|
|
|
|
2022-01-05 16:34:01 -08:00
|
|
|
if (isPasswordlessUser) {
|
2022-01-06 15:27:05 -08:00
|
|
|
await changePassword(password);
|
2022-01-05 16:34:01 -08:00
|
|
|
} else {
|
|
|
|
const recaptchaResponse = await execute();
|
|
|
|
await register(userName, password, recaptchaResponse);
|
2021-12-20 15:56:39 -08:00
|
|
|
}
|
2021-12-16 16:43:35 -08:00
|
|
|
}
|
2022-01-05 16:34:01 -08:00
|
|
|
|
|
|
|
submit()
|
|
|
|
.then(() => {
|
|
|
|
if (location.state && location.state.from) {
|
|
|
|
history.push(location.state.from);
|
|
|
|
} else {
|
|
|
|
history.push("/");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setError(error);
|
|
|
|
setRegistering(false);
|
|
|
|
reset();
|
|
|
|
});
|
2021-08-20 16:23:12 -07:00
|
|
|
},
|
2021-12-20 15:56:39 -08:00
|
|
|
[
|
|
|
|
register,
|
|
|
|
changePassword,
|
|
|
|
location,
|
|
|
|
history,
|
|
|
|
isPasswordlessUser,
|
2022-01-05 16:34:01 -08:00
|
|
|
reset,
|
|
|
|
execute,
|
2021-12-20 15:56:39 -08:00
|
|
|
]
|
2021-08-20 16:23:12 -07:00
|
|
|
);
|
|
|
|
|
2021-12-15 11:10:38 -08:00
|
|
|
useEffect(() => {
|
2021-12-16 16:43:35 -08:00
|
|
|
if (!confirmPasswordRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-15 11:10:38 -08:00
|
|
|
if (password && passwordConfirmation && password !== passwordConfirmation) {
|
|
|
|
confirmPasswordRef.current.setCustomValidity("Passwords must match");
|
|
|
|
} else {
|
|
|
|
confirmPasswordRef.current.setCustomValidity("");
|
|
|
|
}
|
|
|
|
}, [password, passwordConfirmation]);
|
|
|
|
|
2021-12-16 16:43:35 -08:00
|
|
|
useEffect(() => {
|
|
|
|
if (!loading && isAuthenticated && !isPasswordlessUser) {
|
|
|
|
history.push("/");
|
|
|
|
}
|
|
|
|
}, [history, isAuthenticated, isPasswordlessUser]);
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <LoadingView />;
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:23:12 -07:00
|
|
|
return (
|
|
|
|
<>
|
2021-12-10 12:46:18 -08:00
|
|
|
<div className={styles.container}>
|
|
|
|
<div className={styles.content}>
|
|
|
|
<div className={styles.formContainer}>
|
|
|
|
<Logo width="auto" height="auto" className={styles.logo} />
|
|
|
|
<h2>Create your account</h2>
|
|
|
|
<form onSubmit={onSubmitRegisterForm}>
|
|
|
|
<FieldRow>
|
|
|
|
<InputField
|
|
|
|
type="text"
|
2021-12-16 16:43:35 -08:00
|
|
|
name="userName"
|
2021-12-10 12:46:18 -08:00
|
|
|
placeholder="Username"
|
|
|
|
label="Username"
|
|
|
|
autoCorrect="off"
|
|
|
|
autoCapitalize="none"
|
2021-12-15 10:54:01 -08:00
|
|
|
prefix="@"
|
2021-12-17 16:30:10 -08:00
|
|
|
suffix={`:${defaultHomeserverHost}`}
|
2021-12-16 16:43:35 -08:00
|
|
|
value={
|
|
|
|
isAuthenticated && isPasswordlessUser
|
|
|
|
? client.getUserIdLocalpart()
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
disabled={isAuthenticated && isPasswordlessUser}
|
2021-12-10 12:46:18 -08:00
|
|
|
/>
|
|
|
|
</FieldRow>
|
|
|
|
<FieldRow>
|
|
|
|
<InputField
|
2021-12-15 11:10:38 -08:00
|
|
|
required
|
2021-12-16 16:43:35 -08:00
|
|
|
name="password"
|
2021-12-10 12:46:18 -08:00
|
|
|
type="password"
|
2021-12-15 11:10:38 -08:00
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
value={password}
|
2021-12-10 12:46:18 -08:00
|
|
|
placeholder="Password"
|
|
|
|
label="Password"
|
|
|
|
/>
|
|
|
|
</FieldRow>
|
2021-12-15 11:10:38 -08:00
|
|
|
<FieldRow>
|
|
|
|
<InputField
|
|
|
|
required
|
|
|
|
type="password"
|
2021-12-16 16:43:35 -08:00
|
|
|
name="passwordConfirmation"
|
2021-12-15 11:10:38 -08:00
|
|
|
onChange={(e) => setPasswordConfirmation(e.target.value)}
|
|
|
|
value={passwordConfirmation}
|
|
|
|
placeholder="Confirm Password"
|
|
|
|
label="Confirm Password"
|
|
|
|
ref={confirmPasswordRef}
|
|
|
|
/>
|
|
|
|
</FieldRow>
|
2021-12-20 15:56:39 -08:00
|
|
|
{!isPasswordlessUser && (
|
2022-01-05 16:34:01 -08:00
|
|
|
<Caption>
|
|
|
|
This site is protected by ReCAPTCHA and the Google{" "}
|
|
|
|
<Link href="https://www.google.com/policies/privacy/">
|
2021-12-20 15:56:39 -08:00
|
|
|
Privacy Policy
|
2022-01-05 16:34:01 -08:00
|
|
|
</Link>{" "}
|
|
|
|
and{" "}
|
|
|
|
<Link href="https://policies.google.com/terms">
|
|
|
|
Terms of Service
|
|
|
|
</Link>{" "}
|
|
|
|
apply.
|
|
|
|
<br />
|
2022-02-02 14:31:11 -08:00
|
|
|
By clicking "Log in", you agree to our{" "}
|
2022-01-05 16:34:01 -08:00
|
|
|
<Link href={privacyPolicyUrl}>Terms and conditions</Link>
|
|
|
|
</Caption>
|
2021-12-20 15:56:39 -08:00
|
|
|
)}
|
2021-12-10 12:46:18 -08:00
|
|
|
{error && (
|
2021-08-20 16:23:12 -07:00
|
|
|
<FieldRow>
|
2021-12-10 12:46:18 -08:00
|
|
|
<ErrorMessage>{error.message}</ErrorMessage>
|
2021-08-20 16:23:12 -07:00
|
|
|
</FieldRow>
|
2021-12-10 12:46:18 -08:00
|
|
|
)}
|
|
|
|
<FieldRow>
|
2021-12-16 16:43:35 -08:00
|
|
|
<Button type="submit" disabled={registering}>
|
|
|
|
{registering ? "Registering..." : "Register"}
|
2021-12-10 12:46:18 -08:00
|
|
|
</Button>
|
|
|
|
</FieldRow>
|
2022-01-05 16:34:01 -08:00
|
|
|
<div id={recaptchaId} />
|
2021-12-10 12:46:18 -08:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<div className={styles.authLinks}>
|
|
|
|
<p>Already have an account?</p>
|
|
|
|
<p>
|
|
|
|
<Link to="/login">Log in</Link>
|
|
|
|
{" Or "}
|
|
|
|
<Link to="/">Access as a guest</Link>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-08-20 16:23:12 -07:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|