-
Notifications
You must be signed in to change notification settings - Fork 401
/
SignUp.js
131 lines (122 loc) · 3.48 KB
/
SignUp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React from "react"
import { navigate} from "@reach/router"
import { Link } from 'gatsby'
import Error from './Error'
import { Auth } from 'aws-amplify'
const initialState = {
username: ``,
password: ``,
email: '',
phone_number: '',
authCode: '',
stage: 0,
error: ''
}
class SignUp extends React.Component {
state = initialState
handleUpdate = (event) => {
this.setState({
[event.target.name]: event.target.value,
})
}
signUp = async() => {
const { username, password, email, phone_number } = this.state
try {
await Auth.signUp({ username, password, attributes: { email, phone_number }})
this.setState({ stage: 1 })
} catch (err) {
this.setState({ error: err })
console.log('error signing up...', err)
}
}
confirmSignUp = async() => {
const { username, authCode } = this.state
try {
await Auth.confirmSignUp(username, authCode)
alert('Successfully signed up!')
navigate("/app/login")
} catch (err) {
this.setState({ error: err })
console.log('error confirming signing up...', err)
}
}
render() {
return (
<div>
<h1>Sign Up</h1>
{
this.state.stage === 0 && (
<div style={styles.formContainer}>
{this.state.error && <Error errorMessage={this.state.error}/>}
<input
onChange={this.handleUpdate}
placeholder='Username'
name='username'
value={this.state.username}
style={styles.input}
/>
<input
onChange={this.handleUpdate}
placeholder='Password'
name='password'
value={this.state.password}
type='password'
style={styles.input}
/>
<input
onChange={this.handleUpdate}
placeholder='Email'
name='email'
value={this.state.email}
style={styles.input}
/>
<input
onChange={this.handleUpdate}
placeholder='Phone Number'
name='phone_number'
value={this.state.phone_number}
style={styles.input}
/>
<div style={styles.button} onClick={this.signUp}>
<span style={styles.buttonText}>Sign Up</span>
</div>
</div>
)
}
{
this.state.stage === 1 && (
<div style={styles.formContainer}>
{this.state.error && <Error errorMessage={this.state.error}/>}
<input
onChange={this.handleUpdate}
placeholder='Authorization Code'
name='authCode'
value={this.state.authCode}
style={styles.input}
/>
<div style={styles.button} onClick={this.confirmSignUp}>
<span style={styles.buttonText}>Confirm Sign Up</span>
</div>
</div>
)
}
<Link to="/app/login">Sign In</Link>
</div>
)
}
}
const styles = {
input: {
height: 40, margin: '10px 0px', padding: 7
},
formContainer: {
display: 'flex', flexDirection: 'column'
},
button: {
backgroundColor: 'rebeccapurple', padding: '15px 7px', cursor: 'pointer', textAlign: 'center', marginBottom: 10
},
buttonText: {
color: 'white'
}
}
export default SignUp