|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { Auth } from "aws-amplify"; |
| 3 | +import { |
| 4 | + HelpBlock, |
| 5 | + FormGroup, |
| 6 | + FormControl, |
| 7 | + ControlLabel |
| 8 | +} from "react-bootstrap"; |
| 9 | +import LoaderButton from "../components/LoaderButton"; |
| 10 | +import { useFormFields } from "../libs/hooksLib"; |
| 11 | +import "./Signup.css"; |
| 12 | + |
| 13 | +export default function Signup(props) { |
| 14 | + const [fields, handleFieldChange] = useFormFields({ |
| 15 | + email: "", |
| 16 | + password: "", |
| 17 | + confirmPassword: "", |
| 18 | + confirmationCode: "" |
| 19 | + }); |
| 20 | + const [newUser, setNewUser] = useState(null); |
| 21 | + const [isLoading, setIsLoading] = useState(false); |
| 22 | + |
| 23 | + function validateForm() { |
| 24 | + return ( |
| 25 | + fields.email.length > 0 && |
| 26 | + fields.password.length > 0 && |
| 27 | + fields.password === fields.confirmPassword |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + function validateConfirmationForm() { |
| 32 | + return fields.confirmationCode.length > 0; |
| 33 | + } |
| 34 | + |
| 35 | + async function handleSubmit(event) { |
| 36 | + event.preventDefault(); |
| 37 | + |
| 38 | + setIsLoading(true); |
| 39 | + |
| 40 | + try { |
| 41 | + const newUser = await Auth.signUp({ |
| 42 | + username: fields.email, |
| 43 | + password: fields.password |
| 44 | + }); |
| 45 | + setIsLoading(false); |
| 46 | + setNewUser(newUser); |
| 47 | + } catch (e) { |
| 48 | + alert(e.message); |
| 49 | + setIsLoading(false); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + async function handleConfirmationSubmit(event) { |
| 54 | + event.preventDefault(); |
| 55 | + |
| 56 | + setIsLoading(true); |
| 57 | + |
| 58 | + try { |
| 59 | + await Auth.confirmSignUp(fields.email, fields.confirmationCode); |
| 60 | + await Auth.signIn(fields.email, fields.password); |
| 61 | + |
| 62 | + props.userHasAuthenticated(true); |
| 63 | + props.history.push("/"); |
| 64 | + } catch (e) { |
| 65 | + alert(e.message); |
| 66 | + setIsLoading(false); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + function renderConfirmationForm() { |
| 71 | + return ( |
| 72 | + <form onSubmit={handleConfirmationSubmit}> |
| 73 | + <FormGroup controlId="confirmationCode" bsSize="large"> |
| 74 | + <ControlLabel>Confirmation Code</ControlLabel> |
| 75 | + <FormControl |
| 76 | + autoFocus |
| 77 | + type="tel" |
| 78 | + onChange={handleFieldChange} |
| 79 | + value={fields.confirmationCode} |
| 80 | + /> |
| 81 | + <HelpBlock>Please check your email for the code.</HelpBlock> |
| 82 | + </FormGroup> |
| 83 | + <LoaderButton |
| 84 | + block |
| 85 | + type="submit" |
| 86 | + bsSize="large" |
| 87 | + isLoading={isLoading} |
| 88 | + disabled={!validateConfirmationForm()} |
| 89 | + > |
| 90 | + Verify |
| 91 | + </LoaderButton> |
| 92 | + </form> |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + function renderForm() { |
| 97 | + return ( |
| 98 | + <form onSubmit={handleSubmit}> |
| 99 | + <FormGroup controlId="email" bsSize="large"> |
| 100 | + <ControlLabel>Email</ControlLabel> |
| 101 | + <FormControl |
| 102 | + autoFocus |
| 103 | + type="email" |
| 104 | + value={fields.email} |
| 105 | + onChange={handleFieldChange} |
| 106 | + /> |
| 107 | + </FormGroup> |
| 108 | + <FormGroup controlId="password" bsSize="large"> |
| 109 | + <ControlLabel>Password</ControlLabel> |
| 110 | + <FormControl |
| 111 | + type="password" |
| 112 | + value={fields.password} |
| 113 | + onChange={handleFieldChange} |
| 114 | + /> |
| 115 | + </FormGroup> |
| 116 | + <FormGroup controlId="confirmPassword" bsSize="large"> |
| 117 | + <ControlLabel>Confirm Password</ControlLabel> |
| 118 | + <FormControl |
| 119 | + type="password" |
| 120 | + onChange={handleFieldChange} |
| 121 | + value={fields.confirmPassword} |
| 122 | + /> |
| 123 | + </FormGroup> |
| 124 | + <LoaderButton |
| 125 | + block |
| 126 | + type="submit" |
| 127 | + bsSize="large" |
| 128 | + isLoading={isLoading} |
| 129 | + disabled={!validateForm()} |
| 130 | + > |
| 131 | + Signup |
| 132 | + </LoaderButton> |
| 133 | + </form> |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return ( |
| 138 | + <div className="Signup"> |
| 139 | + {newUser === null ? renderForm() : renderConfirmationForm()} |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments