Add password confirmation input

This commit is contained in:
Robert Long 2021-12-15 11:10:38 -08:00
parent 382ca2baa4
commit b57eaee5ca

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, { useCallback, useRef, useState } from "react"; import React, { useCallback, useEffect, useRef, useState } from "react";
import { useHistory, useLocation, Link } from "react-router-dom"; import { useHistory, useLocation, Link } from "react-router-dom";
import { FieldRow, InputField, ErrorMessage } from "./Input"; import { FieldRow, InputField, ErrorMessage } from "./Input";
import { Button } from "./button"; import { Button } from "./button";
@ -23,14 +23,15 @@ import styles from "./LoginPage.module.css";
import { ReactComponent as Logo } from "./icons/LogoLarge.svg"; import { ReactComponent as Logo } from "./icons/LogoLarge.svg";
export function RegisterPage() { export function RegisterPage() {
// TODO: Handle hitting login page with authenticated client
const { register } = useClient(); const { register } = useClient();
const registerUsernameRef = useRef(); const registerUsernameRef = useRef();
const registerPasswordRef = useRef(); const confirmPasswordRef = useRef();
const history = useHistory(); const history = useHistory();
const location = useLocation(); const location = useLocation();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState(); const [error, setError] = useState();
const [password, setPassword] = useState("");
const [passwordConfirmation, setPasswordConfirmation] = useState("");
const onSubmitRegisterForm = useCallback( const onSubmitRegisterForm = useCallback(
(e) => { (e) => {
@ -55,6 +56,14 @@ export function RegisterPage() {
[register, location, history] [register, location, history]
); );
useEffect(() => {
if (password && passwordConfirmation && password !== passwordConfirmation) {
confirmPasswordRef.current.setCustomValidity("Passwords must match");
} else {
confirmPasswordRef.current.setCustomValidity("");
}
}, [password, passwordConfirmation]);
return ( return (
<> <>
<div className={styles.container}> <div className={styles.container}>
@ -77,12 +86,25 @@ export function RegisterPage() {
</FieldRow> </FieldRow>
<FieldRow> <FieldRow>
<InputField <InputField
required
type="password" type="password"
ref={registerPasswordRef} onChange={(e) => setPassword(e.target.value)}
value={password}
placeholder="Password" placeholder="Password"
label="Password" label="Password"
/> />
</FieldRow> </FieldRow>
<FieldRow>
<InputField
required
type="password"
onChange={(e) => setPasswordConfirmation(e.target.value)}
value={passwordConfirmation}
placeholder="Confirm Password"
label="Confirm Password"
ref={confirmPasswordRef}
/>
</FieldRow>
{error && ( {error && (
<FieldRow> <FieldRow>
<ErrorMessage>{error.message}</ErrorMessage> <ErrorMessage>{error.message}</ErrorMessage>