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

[Doc] Explain how <PasswordInput> can be used to update a password #9354

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions docs/PasswordInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ title: "The PasswordInput Component"
Use it like a [`<TextInput>`](./TextInput.md):

```jsx
import { PasswordInput } from 'react-admin';

<PasswordInput source="password" />
import { Create, SimpleForm, TextInput, PasswordInput } from 'react-admin';

export const UserCreate = () => (
<Create>
<SimpleForm>
<TextInput source="name" />
<TextInput source="email" />
<PasswordInput source="password" />
</SimpleForm>
</Create>
);
```

**Tip**: Your API should never send the password in any of its responses, because the API backend shouldn't store the password in clear. In particular, the response to the `dataProvider.create()` call should not contain the password passed as input.

## Props

| Prop | Required | Type | Default | Description |
Expand Down Expand Up @@ -47,3 +57,47 @@ Set the [`autocomplete` attribute](https://developer.mozilla.org/en-US/docs/Web/
<PasswordInput source="password" inputProps={{ autocomplete: 'current-password' }} />
```
{% endraw %}

Copy link
Member Author

Choose a reason for hiding this comment

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

We probably need to explain how to have 2 password inputs, and how to validate that they are equal.

Copy link
Member Author

Choose a reason for hiding this comment

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

explained.

## Validating Identical Passwords

If you want to validate that the user has entered the same password in two different password inputs, use a [custom function validator](./Validation.md#per-input-validation-custom-function-validator):

```jsx
import { Create, SimpleForm, TextInput, PasswordInput } from 'react-admin';

const equalToPassword = (value, allValues) => {
if (value !== allValues.password) {
return 'The two passwords must match';
}
}

export const UserCreate = () => (
<Create>
<SimpleForm>
<TextInput source="name" />
<TextInput source="email" />
<PasswordInput source="password" />
<PasswordInput source="confirm_password" validate={equalToPassword} />
</SimpleForm>
</Create>
);
```

## Usage in Edit Views

You may want to allow users to *update* a password on an existing record. The usual solution to this is to include a `new_password` input in the Edition form. Your API should then check if this field is present in the payload, and update the password accordingly.

```jsx
import { Edit, SimpleForm, TextInput, PasswordInput } from 'react-admin';

export const UserEdit = () => (
<Edit>
<SimpleForm>
<TextInput source="name" />
<TextInput source="email" />
<PasswordInput source="new_password" />
</SimpleForm>
</Edit>
);
```

Loading