forked from vercel/next.js
/
signup.js
109 lines (92 loc) · 2.44 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
import React, { useState } from 'react'
import Layout from '../components/layout'
import { login } from '../utils/auth'
function Signup() {
const [userData, setUserData] = useState({
email: '',
password: '',
error: '',
})
async function handleSubmit(event) {
event.preventDefault()
setUserData({ ...userData, error: '' })
const email = userData.email
const password = userData.password
try {
const response = await fetch('/api/signup', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
})
if (response.status !== 200) {
throw new Error(await response.text())
}
const data = await response.json()
login({ email: data.email })
} catch (error) {
console.error(error)
setUserData({ ...userData, error: error.message })
}
}
return (
<Layout>
<div className="signup">
<form onSubmit={handleSubmit}>
<label htmlFor="email">Email</label>
<input
type="text"
id="email"
name="email"
value={userData.email}
onChange={event =>
setUserData(
Object.assign({}, userData, { email: event.target.value })
)
}
/>
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
name="password"
value={userData.password}
onChange={event =>
setUserData(
Object.assign({}, userData, { password: event.target.value })
)
}
/>
<button type="submit">Sign up</button>
{userData.error && <p className="error">Error: {userData.error}</p>}
</form>
</div>
<style jsx>{`
.signup {
max-width: 340px;
margin: 0 auto;
padding: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
form {
display: flex;
flex-flow: column;
}
label {
font-weight: 600;
}
input {
padding: 8px;
margin: 0.3rem 0 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
.error {
margin: 0.5rem 0 0;
color: brown;
}
`}</style>
</Layout>
)
}
export default Signup