Create the Signup Form

Let's start by creating the signup form that'll get the user's email and password.

Add the Container

Create a new container at src/containers/Signup.tsx with the following.

import React, { useState } from "react";
import {
HelpBlock,
FormGroup,
FormControl,
ControlLabel
} from "react-bootstrap";
import LoaderButton from "../components/LoaderButton";
import { useFormFields } from "../libs/hooksLib";
import "./Signup.css";
import { ISignUpResult } from "amazon-cognito-identity-js";
import { AppProps } from "../App";
import { RouteComponentProps, useHistory } from "react-router-dom";
interface SignupProps extends AppProps, RouteComponentProps {}
const Signup: React.FC<SignupProps> = props => {
const [fields, handleFieldChange] = useFormFields({
email: "",
password: "",
confirmPassword: "",
confirmationCode: ""
});
const [newUser, setNewUser] = useState((null as unknown) as ISignUpResult);
const [isLoading, setIsLoading] = useState(false);
function validateForm() {
return (
fields.email.length > 0 &&
fields.password.length > 0 &&
fields.password === fields.confirmPassword
);
}
function validateConfirmationForm() {
return fields.confirmationCode.length > 0;
}
async function handleSubmit(event) {
event.preventDefault();
setIsLoading(true);
setNewUser("test");
setIsLoading(false);
}
async function handleConfirmationSubmit(event) {
event.preventDefault();
setIsLoading(true);
}
function renderConfirmationForm() {
return (
<form onSubmit={handleConfirmationSubmit}>
<FormGroup controlId="confirmationCode" bsSize="large">
<ControlLabel>Confirmation Code</ControlLabel>
<FormControl
autoFocus
type="tel"
onChange={handleFieldChange}
value={fields.confirmationCode}
/>
<HelpBlock>Please check your email for the code.</HelpBlock>
</FormGroup>
<LoaderButton
block
type="submit"
bsSize="large"
isLoading={isLoading}
disabled={!validateConfirmationForm()}
>
Verify
</LoaderButton>
</form>
);
}
function renderForm() {
return (
<form onSubmit={handleSubmit}>
<FormGroup controlId="email" bsSize="large">
<ControlLabel>Email</ControlLabel>
<FormControl
autoFocus
type="email"
value={fields.email}
onChange={handleFieldChange}
/>
</FormGroup>
<FormGroup controlId="password" bsSize="large">
<ControlLabel>Password</ControlLabel>
<FormControl
type="password"
value={fields.password}
onChange={handleFieldChange}
/>
</FormGroup>
<FormGroup controlId="confirmPassword" bsSize="large">
<ControlLabel>Confirm Password</ControlLabel>
<FormControl
type="password"
onChange={handleFieldChange}
value={fields.confirmPassword}
/>
</FormGroup>
<LoaderButton
block
type="submit"
bsSize="large"
isLoading={isLoading}
disabled={!validateForm()}
>
Signup
</LoaderButton>
</form>
);
}
return (
<div className="Signup">
{newUser === null ? renderForm() : renderConfirmationForm()}
</div>
);
}
export default Signup;

Most of the things we are doing here are fairly straightforward but let's go over them quickly.

  1. Since we need to show the user a form to enter the confirmation code, we are conditionally rendering two forms based on if we have a user object or not.

  2. We are using the LoaderButton component that we created earlier for our submit buttons.

  3. Since we have two forms we have two validation functions called validateForm and validateConfirmationForm.

  4. We are setting the autoFocus flags on the email and the confirmation code fields.

  5. For now our handleSubmit and handleConfirmationSubmit don't do a whole lot besides setting the isLoading state and a dummy value for the newUser state.

  6. And you'll notice we are using the useFormFields custom React Hook that we [previously created]({% link _chapters/create-a-custom-react-hook-to-handle-form-fields.md %}) to handle our form fields.

Also, let's add a couple of styles in src/containers/Signup.css.

@media all and (min-width: 480px) {
.Signup {
padding: 60px 0;
}
.Signup form {
margin: 0 auto;
max-width: 320px;
}
}
.Signup form span.help-block {
font-size: 14px;
padding-bottom: 10px;
color: #999;
}

Add the Route

Finally, add our container as a route in src/Routes.js below our login route. We are using the AppliedRoute component that we created in the [Add the session to the state]({% link _chapters/add-the-session-to-the-state.md %}) chapter.

<AppliedRoute path="/signup" exact component={Signup} appProps={appProps} />

And include our component in the header.

import Signup from "./containers/Signup";

Now if we switch to our browser and navigate to the signup page we should see our newly created form. Our form doesn't do anything when we enter in our info but you can still try to fill in an email address, password, and the confirmation code. It'll give you an idea of how the form will behave once we connect it to Cognito.

Signup page added screenshot

Next, let's connect our signup form to Amazon Cognito.