Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoginWithEmail to facilitate login with email #10518

Merged
merged 5 commits into from
Feb 18, 2025
Merged

Add LoginWithEmail to facilitate login with email #10518

merged 5 commits into from
Feb 18, 2025

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Feb 15, 2025

Problem

To create a custom login form (e.g., to use email/password instead of userrname/password), developers have to copy an enorm amount of code from react-admin.

Solution

Provide a built-in LoginWithEmail component:

import { Admin, LoginWithEmail } from 'react-admin';
import authProvider from './authProvider';

const App = () => (
    <Admin loginPage={LoginWithEmail} authProvider={authProvider}>
        ...
    </Admin>
);

LoginWithEmail

Improve composition of Login components,to allow to easily add custom content.

import { Box, Link } from '@mui/material';
import { Link as RouterLink } from 'react-router-dom';
import { Login, LoginForm } from 'react-admin';

const MyLogin = () => (
    <Login>
        <LoginForm />
        <Box textAlign="center" mb={1}>
            <Link component={RouterLink} to="/forgot-password">
                Forgot password?
            </Link>
        </Box>
    </Login>
);

LoginWithContent

I also updated the default login form to use a <PasswordInput> instead of a <TextInput type="password"> so that we benefit from the "show/hide password" button.

How To Test

  • The custom login forms of the eCommerce and CRM demos still work, even though 90% of their code was removed.
  • The Login story was expanded to cover more cases
  • Addes a LoginWithEmail story

Additional Checks

  • The PR targets master for a bugfix, or next for a feature
  • [ ] The PR includes unit tests (current tests are enough)
  • The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

@fzaninotto fzaninotto changed the title Login--form Update Login component to facilitate login form customization Feb 15, 2025
@fzaninotto fzaninotto added the RFR Ready For Review label Feb 15, 2025
@fzaninotto fzaninotto changed the title Update Login component to facilitate login form customization Add LoginWithEmail to facilitate login with email Feb 15, 2025
autoComplete="username"
validate={required()}
/>
<PasswordInput
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice: I changed TextInput for PasswordInput here.

};

type FieldValues = Record<string, any>;
export const LoginForm = () => (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't use LoginWithEmail here as we also add the "forgot password" link.

@djhi djhi self-requested a review February 17, 2025 08:45
Copy link
Collaborator

@djhi djhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement!

Comment on lines +12 to +27
<LoginForm>
<TextInput
autoFocus
source="email"
label={translate('ra.auth.email', { _: 'Email' })}
autoComplete="email"
type="email"
validate={required()}
/>
<PasswordInput
source="password"
label={translate('ra.auth.password', { _: 'Password' })}
autoComplete="current-password"
validate={required()}
/>
</LoginForm>
Copy link
Collaborator

@djhi djhi Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have a LoginFormWithEmail to make customization even easier (like adding custom content in the simple example)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be too many components (not easy to document). Instead, I documented how to pass custom children.

@djhi djhi added this to the 5.6.0 milestone Feb 18, 2025
@djhi djhi merged commit dd31657 into next Feb 18, 2025
16 checks passed
@djhi djhi deleted the login--form branch February 18, 2025 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants