Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/customization/adding_attributes_to_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ php spark db:table users

See [Customizing User Provider](./user_provider.md).

Don't forget to add the added attributes to the `$allowedFields` property.

## Update Validation Rules

You need to update the [validation rules](./validation_rules.md) for registration.
Expand Down
38 changes: 37 additions & 1 deletion docs/customization/user_provider.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Customizing User Provider

## Creating Your Own UserModel

If you want to customize user attributes, you need to create your own
[User Provider](../getting_started/concepts.md#user-providers) class.
The only requirement is that your new class MUST extend the provided `CodeIgniter\Shield\Models\UserModel`.
Expand All @@ -13,8 +15,42 @@ php spark shield:model UserModel

The class name is optional. If none is provided, the generated class name would be `UserModel`.

After creating the class, set the `$userProvider` property in **app/Config/Auth.php** as follows:
## Configuring to Use Your UserModel

After creating the class, set your model classname to the `$userProvider` property
in **app/Config/Auth.php**:

```php
public string $userProvider = \App\Models\UserModel::class;
```

## Customizing Your UserModel

Customize your model as you like.

If you add attributes, don't forget to add the attributes to the `$allowedFields`
property.

```php
<?php

declare(strict_types=1);

namespace App\Models;

use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;

class UserModel extends ShieldUserModel
{
protected function initialize(): void
{
parent::initialize();

$this->allowedFields = [
...$this->allowedFields,
'first_name', // Added
'last_name', // Added
];
}
}
```