-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathEmailAuth.tsx
246 lines (234 loc) · 7.07 KB
/
EmailAuth.tsx
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { SupabaseClient } from '@supabase/supabase-js'
import React, { useEffect, useRef, useState } from 'react'
import {
I18nVariables,
RedirectTo,
ViewSignUp,
ViewSignIn,
VIEWS,
ViewType,
} from '@supabase/auth-ui-shared'
import { Appearance } from './../../../types'
import {
Anchor,
Button,
Container,
Input,
Label,
Message,
} from './../../UI/index.js'
export interface EmailAuthProps {
authView?: ViewSignIn | ViewSignUp
defaultEmail?: string
defaultPassword?: string
setAuthView?: any
setDefaultEmail?: (email: string) => void
setDefaultPassword?: (password: string) => void
supabaseClient: SupabaseClient
showLinks?: boolean
redirectTo?: RedirectTo
additionalData?: { [key: string]: any }
magicLink?: boolean
i18n?: I18nVariables
appearance?: Appearance
passwordLimit?: boolean
children?: React.ReactNode
}
function EmailAuth({
authView = 'sign_in',
defaultEmail = '',
defaultPassword = '',
setAuthView = () => {},
setDefaultEmail = (email) => {},
setDefaultPassword = (password) => {},
supabaseClient,
showLinks = false,
redirectTo,
additionalData,
magicLink,
i18n,
appearance,
passwordLimit = false,
children,
}: EmailAuthProps) {
const isMounted = useRef<boolean>(true)
const [email, setEmail] = useState(defaultEmail)
const [password, setPassword] = useState(defaultPassword)
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const [message, setMessage] = useState('')
useEffect(() => {
isMounted.current = true
setEmail(defaultEmail)
setPassword(defaultPassword)
return () => {
isMounted.current = false
}
}, [authView])
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
setError('')
setLoading(true)
switch (authView) {
case 'sign_in':
const { error: signInError } =
await supabaseClient.auth.signInWithPassword({
email,
password,
})
if (signInError) setError(signInError.message)
break
case 'sign_up':
if (passwordLimit && password.length > 72) {
setError('Password exceeds maxmium length of 72 characters')
return
}
let options: { emailRedirectTo: RedirectTo; data?: object } = {
emailRedirectTo: redirectTo,
}
if (additionalData) {
options.data = additionalData
}
const {
data: { user: signUpUser, session: signUpSession },
error: signUpError,
} = await supabaseClient.auth.signUp({
email,
password,
options,
})
if (signUpError) setError(signUpError.message)
// Check if session is null -> email confirmation setting is turned on
else if (signUpUser && !signUpSession)
setMessage(i18n?.sign_up?.confirmation_text as string)
break
}
/*
* it is possible the auth component may have been unmounted at this point
* check if component is mounted before setting a useState
*/
if (isMounted.current) setLoading(false)
}
const handleViewChange = (newView: ViewType) => {
setDefaultEmail(email)
setDefaultPassword(password)
setAuthView(newView)
}
const labels = i18n?.[authView]
return (
<form
id={authView === 'sign_in' ? `auth-sign-in` : `auth-sign-up`}
onSubmit={handleSubmit}
autoComplete={'on'}
style={{ width: '100%' }}
>
<Container direction="vertical" gap="large" appearance={appearance}>
<Container direction="vertical" gap="large" appearance={appearance}>
<div>
<Label htmlFor="email" appearance={appearance}>
{labels?.email_label}
</Label>
<Input
id="email"
type="email"
name="email"
placeholder={labels?.email_input_placeholder}
defaultValue={email}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setEmail(e.target.value)
}
autoComplete="email"
appearance={appearance}
/>
</div>
<div>
<Label htmlFor="password" appearance={appearance}>
{labels?.password_label}
</Label>
<Input
id="password"
type="password"
name="password"
placeholder={labels?.password_input_placeholder}
defaultValue={password}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setPassword(e.target.value)
}
autoComplete={
authView === 'sign_in' ? 'current-password' : 'new-password'
}
appearance={appearance}
/>
</div>
{children}
</Container>
<Button
type="submit"
color="primary"
loading={loading}
appearance={appearance}
>
{loading ? labels?.loading_button_label : labels?.button_label}
</Button>
{showLinks && (
<Container direction="vertical" gap="small" appearance={appearance}>
{authView === VIEWS.SIGN_IN && magicLink && (
<Anchor
href="#auth-magic-link"
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
setAuthView(VIEWS.MAGIC_LINK)
}}
appearance={appearance}
>
{i18n?.magic_link?.link_text}
</Anchor>
)}
{authView === VIEWS.SIGN_IN && (
<Anchor
href="#auth-forgot-password"
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
setAuthView(VIEWS.FORGOTTEN_PASSWORD)
}}
appearance={appearance}
>
{i18n?.forgotten_password?.link_text}
</Anchor>
)}
{authView === VIEWS.SIGN_IN ? (
<Anchor
href="#auth-sign-up"
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
handleViewChange(VIEWS.SIGN_UP)
}}
appearance={appearance}
>
{i18n?.sign_up?.link_text}
</Anchor>
) : (
<Anchor
href="#auth-sign-in"
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
handleViewChange(VIEWS.SIGN_IN)
}}
appearance={appearance}
>
{i18n?.sign_in?.link_text}
</Anchor>
)}
</Container>
)}
</Container>
{message && <Message appearance={appearance}>{message}</Message>}
{error && (
<Message color="danger" appearance={appearance}>
{error}
</Message>
)}
</form>
)
}
export { EmailAuth }