import React, { useCallback, useEffect, useState } from 'react'; import { useAuth, useLogin, useQueryParams, ValidatorProvider } from 'react-admin-base'; import { FormattedMessage, useIntl } from "react-intl"; import { Link } from 'react-router-dom'; import { Alert, Button, Card, CardBody, Form, FormGroup, Input, Label } from 'reactstrap'; import { Validator, LoadingButton } from "react-admin-base-bootstrap"; import Layout from '../Layout'; const error_messages = { "invalid_grant": "INVALID_PASSWORD" }; export default function Login({ children } : { children?: React.ReactNode }) { const [ api ] = useAuth(); const login = useLogin(); const intl = useIntl(); const { code } = useQueryParams(); const [ username, setUsername] = useState(''); const [ password, setPassword] = useState(''); const [ loading, setLoading] = useState(false); const [ error, setError ] = useState(null); const loginByCode = useCallback(async function() { if (code) { setLoading(true); try { await api.log_in_by_code(code, window.location.origin + login.login); } catch(e) { setError(e); } finally { setLoading(false); } } }, [setLoading, setError, code]); useEffect(function() { loginByCode(); }, [loginByCode]); const handleSubmit = useCallback(async function(e) { e.preventDefault(); if (loading) return false; setError(null); setLoading(true); try { await api.log_in(username, password); } catch(e) { console.error(e); const msg = error_messages[(e && e.message) || '-']; if (msg) { setError(new Error(intl.formatMessage({ id: msg }))); } else { setError(e); } } finally { setLoading(false); } }, [username, password, error, loading, intl, error_messages]); if (code) { if (loading) { return ; } } return

{ setUsername(a.currentTarget.value); setError(null); }} disabled={loading} autoComplete="username" /> { setPassword(a.currentTarget.value); setError(null); }} disabled={loading} autoComplete="current-password" /> { login.reset && } { error && setError(null)}>{ error.message } }
{ login.register && } { children &&

- -

{ children }
}
; }