Debian Codegraph @

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

raw
1 import { useState } from 'react'
2 import FormErrors from '../components/FormErrors'
3 import { login } 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 import WebAuthnLoginButton from '../mfa/WebAuthnLoginButton'
9
10 export default function Login () {
11 const [email, setEmail] = useState('')
12 const [password, setPassword] = useState('')
13 const [response, setResponse] = useState({ fetching: false, content: null })
14 const config = useConfig()
15 const hasProviders = config.data.socialaccount?.providers?.length > 0
16
17 function submit () {
18 setResponse({ ...response, fetching: true })
19 login({ email, password }).then((content) => {
20 setResponse((r) => { return { ...r, content } })
21 }).catch((e) => {
22 console.error(e)
23 window.alert(e)
24 }).then(() => {
25 setResponse((r) => { return { ...r, fetching: false } })
26 })
27 }
28 return (
29 <div>
30 <h1>Login</h1>
31 <p>
32 No account? <Link to='/account/signup'>Sign up here.</Link>
33 </p>
34
35 <FormErrors errors={response.content?.errors} />
36
37 <div><label>Email <input value={email} onChange={(e) => setEmail(e.target.value)} type='email' required /></label>
38 <FormErrors param='email' errors={response.content?.errors} />
39 </div>
40 <div><label>Password: <input value={password} onChange={(e) => setPassword(e.target.value)} type='password' required /></label>
41 <Link to='/account/password/reset'>Forgot your password?</Link>
42 <FormErrors param='password' errors={response.content?.errors} />
43 </div>
44 <Button disabled={response.fetching} onClick={() => submit()}>Login</Button>
45 {config.data.account.login_by_code_enabled
46 ? <Link className='btn btn-secondary' to='/account/login/code'>Send me a sign-in code</Link>
47 : null}
48 <WebAuthnLoginButton>Sign in with a passkey</WebAuthnLoginButton>
49 {hasProviders
50 ? <>
51 <h2>Or use a third-party</h2>
52 <ProviderList callbackURL='/account/provider/callback' />
53 </>
54 : null}
55 </div>
56 )
57 }