import { BaseAuthProps } from './types'; import { AuthBackground } from '@/components/AuthBackground'; import { AuthWrapper } from '@/components/AuthWrapper'; import { TextInput } from '@/components/ra-inputs'; import { Button, CircularProgress, Divider, Grid, Link, Stack, Typography } from '@mui/material'; import { Form, required, useCheckAuth, useLogin, useNotify, useSafeSetState, useTranslate } from 'ra-core'; import { useEffect } from 'react'; import { Link as RouterLink, useNavigate } from 'react-router-dom'; import _ from 'lodash'; import { GoogleLoginButton } from '@/components/ra-buttons'; type LoginPageProps = BaseAuthProps & { /** * Indicates whether to enable the password recovery screen. * If enabled, it is advisable to register a route to /recover with a component * that takes care of recovery, you can use the Applica component directly for this * * @see RecoverPage for more information. * @example * import { RecoverPage } from "@applica-software-guru/react-admin"; * * * */ enablePasswordRecover?: boolean; /** * Indicates whether to enable the registration screen. * If enabled, it is advisable to register a route to /register with a component * that takes care of registration, you can use the Applica component directly for this * * @see RegisterPage for more information. * @example * import { RegisterPage } from "@applica-software-guru/react-admin"; * * * */ enableRegistration?: boolean; /** * Indicates the page to redirect the user to after login. * If not specified, the react-admin home page is used. */ redirectTo: string; /** * Indicates which OAuth providers to enable and configurations. */ oauth?: { google?: { clientId: string; }; }; }; function LoginPage({ version, name, copy, logo, enablePasswordRecover = false, enableRegistration = false, oauth = {}, redirectTo, background = }: LoginPageProps): JSX.Element { const [loading, setLoading] = useSafeSetState(false); const login = useLogin(); const translate = useTranslate(); const notify = useNotify(); const checkAuth = useCheckAuth(); const navigate = useNavigate(); useEffect(() => { checkAuth({}, false) .then(() => { navigate('/'); }) .catch(() => { // not authenticated, stay on the login page }); }, [checkAuth, navigate]); function handleSubmit(values: any) { setLoading(true); login(values, redirectTo) .then(() => { setLoading(false); }) .catch((error) => { setLoading(false); notify( typeof error === 'string' ? error : typeof error === 'undefined' || !error.message ? 'ra.auth.sign_in_error' : error.message, { type: 'error', messageArgs: { _: typeof error === 'string' ? error : error && error.message ? error.message : undefined } } ); }); } return ( Login {enableRegistration ? ( {translate('ra.auth.register')} ) : null}
{enablePasswordRecover ? ( {translate('ra.auth.forgot_password')} ) : null}
{!_.isEmpty(oauth) ? ( <> {oauth.google ? ( ) : null} ) : null}
); } export { LoginPage }; export type { LoginPageProps };