Debian Codegraph @

examples/react-spa/frontend/src/account/Signup.js

raw
1 import { useState } from 'react'
2 import FormErrors from '../components/FormErrors'
3 import { signUp } from '../lib/allauth'
4 import { Link } from 'react-router-dom'
5 import { useConfig } from '../auth'
6 import ProviderList from '../socialaccount/ProviderList'
7 import Button from '../components/Button'
8
9 export default function Signup () {
10 const [email, setEmail] = useState('')
11 const [password1, setPassword1] = useState('')
12 const [password2, setPassword2] = useState('')
13 const [password2Errors, setPassword2Errors] = useState([])
14 const [response, setResponse] = useState({ fetching: false, content: null })
15 const config = useConfig()
16 const hasProviders = config.data.socialaccount?.providers?.length > 0
17
18 function submit () {
19 if (password2 !== password1) {
20 setPassword2Errors([{ param: 'password2', message: 'Password does not match.' }])
21 return
22 }
23 setPassword2Errors([])
24 setResponse({ ...response, fetching: true })
25 signUp({ email, password: password1 }).then((content) => {
26 setResponse((r) => { return { ...r, content } })
27 }).catch((e) => {
28 console.error(e)
29 window.alert(e)
30 }).then(() => {
31 setResponse((r) => { return { ...r, fetching: false } })
32 })
33 }
34
35 return (
36 <div>
37 <h1>Sign Up</h1>
38 <p>
39 Already have an account? <Link to='/account/login'>Login here.</Link>
40 </p>
41
42 <FormErrors errors={response.content?.errors} />
43
44 <div><label>Email <input value={email} onChange={(e) => setEmail(e.target.value)} type='email' required /></label>
45 <FormErrors param='email' errors={response.content?.errors} />
46 </div>
47 <div><label>Password: <input autoComplete='new-password' value={password1} onChange={(e) => setPassword1(e.target.value)} type='password' required /></label>
48 <FormErrors param='password' errors={response.content?.errors} />
49 </div>
50 <div><label>Password (again): <input value={password2} onChange={(e) => setPassword2(e.target.value)} type='password' required /></label>
51 <FormErrors param='password2' errors={password2Errors} />
52 </div>
53 <Button disabled={response.fetching} onClick={() => submit()}>Sign Up</Button>
54 <a href='/account/signup/passkey'>Sign up using a passkey</a>
55
56 {hasProviders
57 ? <>
58 <h2>Or use a third-party</h2>
59 <ProviderList callbackURL='/account/provider/callback' />
60 </>
61 : null}
62 </div>
63 )
64 }