-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsignup.tsx
More file actions
225 lines (209 loc) · 7.7 KB
/
signup.tsx
File metadata and controls
225 lines (209 loc) · 7.7 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import Link from 'next/link'
import {useState} from 'react'
import {useForm} from 'react-hook-form'
import {useCreateUserWithEmailAndPassword} from 'react-firebase-hooks/auth'
import VisuallyHidden from '@reach/visually-hidden'
import firebase from 'firebase/app'
import {auth} from '@/lib/firebase'
interface CredentialProps {
credential?: any;
email?: string;
password?: string;
}
const SignUp = () => {
const {
handleSubmit,
register,
errors,
formState: {isValid}
} = useForm({mode: 'onBlur'})
const [createUserWithEmailAndPassword, user, loading, error] = useCreateUserWithEmailAndPassword(
auth
)
const [tempCredentials, setTempCredentials] = useState<CredentialProps>({});
const onSubmit = (values: {
email: string
firstName: string
lastName: string
password: string
passwordConfirmation: string
}) => {
const {email, password} = values
createUserWithEmailAndPassword(email, password)
}
const onGithubSubmit = async () => {
const githubProvider = new firebase.auth.GithubAuthProvider()
const authUser = await auth
.signInWithPopup(githubProvider)
.catch(error => {
// In the event that an account with this email already exists (because they signed up with email)
// Store the existing credential and email conflict, and then ask the user to input the password for their email account
// Otherwise... handleErrors()
if (error.code === 'auth/account-exists-with-different-credential') {
// Store the pending Github credential and conflicting email
setTempCredentials({
credential: error.credential,
email: error.email,
password: null,
});
// TODO: Open the modal to ask for a password
console.log('credential error')
} else {
console.log(error)
}
})
if (authUser) {
// TODO: If we're creating an account for the first time, we need to store some information about the user
// see https://github.com/OpenMined/openmined/blob/dev/apps/courses/src/components/forms/users/SignUp.tsx
let firstName = ''
let lastName = ''
if (authUser.user.displayName && authUser.user.displayName !== '') {
const splitName = authUser.user.displayName.split(' ');
firstName = splitName.length >= 1 ? splitName[0] : authUser.user.displayName;
lastName = splitName.length >= 2 ? splitName.slice(1).join(' ') : ''
}
const profile = authUser.additionalUserInfo.profile as any
authUser.user.updateProfile({
first_name: firstName,
last_name: lastName,
notification_preferences: ['project_reviews'],
description: profile.bio,
github: profile.login,
twitter: profile.twitter_username,
website: profile.blog,
photoURL: profile.avatar_url,
}).then(() => {
// Update successful.
// TODO: change to trigger dashboard redirect here
console.log('user profile update success')
}).catch(error => {
// An error happened.
console.log(error)
})
}
}
if (user) {
// TODO: redirect to dashboard
return (
<p>user created</p>
)
}
if (error) {
// TODO: error handling
}
// TODO: if error, notify user -- use a notification component from OMUI
return (
<article className="container pt-8 mx-auto">
<VisuallyHidden as="h1">Sign up to OpenMined Courses</VisuallyHidden>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 md:gap-8">
<header className="flex flex-col mr-8 space-y-4">
<h2 className="text-5xl">Create your courses account</h2>
<p>
"Tell me and I forget, teach me and I may remember, involve me and I learn." - Benjamin
Franklin
</p>
</header>
<section>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col w-full space-y-4">
<div className="flex flex-col space-y-4 md:space-y-0 md:space-x-4 md:flex-row">
<div className="w-full">
<label htmlFor="firstName">First name</label>
<input
type="text"
id="firstName"
name="firstName"
className="w-full"
placeholder="First name"
ref={register({required: true})}
/>
{errors.firstName && <span>The field is required</span>}
</div>
<div className="w-full">
<label htmlFor="lastName">Last name</label>
<input
type="text"
id="lastName"
name="lastName"
className="w-full"
placeholder="Last name"
ref={register({required: true})}
/>
{errors.lastName && <span>The field is required</span>}
</div>
</div>
<div className="w-full">
<label htmlFor="email">Email</label>
<input
type="email"
id="email"
name="email"
className="w-full"
placeholder="Email address"
ref={register({required: true})}
/>
{errors.email && <span>The field is required</span>}
</div>
<div className="flex flex-col w-full space-y-4 md:space-y-0 md:space-x-4 md:flex-row">
<div className="w-full">
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
name="password"
placeholder="Password"
className="w-full"
ref={register({required: true})}
/>
{errors.password && <span>The field is required</span>}
</div>
<div className="w-full">
<label htmlFor="passwordConfirmation">First name</label>
<input
type="password"
id="passwordConfirmation"
name="passwordConfirmation"
className="w-full"
placeholder="Password confirmation"
ref={register({required: true})}
/>
{errors.passwordConfirmation && <span>Passwords must match</span>}
</div>
</div>
<div className="flex space-x-8">
<button
type="submit"
className="px-4 py-2 text-white bg-black rounded-md"
disabled={!isValid || loading}
>
Sign up
</button>
<button
type="button"
disabled={loading}
className="px-4 py-2 text-white bg-black rounded-md"
onClick={onGithubSubmit}
>
Sign up with GitHub
</button>
</div>
<div className="border" />
<p>
By signing up you agree to our{' '}
<Link href="/terms">
<a className="text-blue-600">Terms of Use</a>
</Link>{' '}
and{' '}
<Link href="/policy">
<a className="text-blue-600">Privacy Policy</a>
</Link>
.
</p>
</div>
</form>
</section>
</div>
</article>
)
}
export default SignUp