You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/authentication.md
+60-2Lines changed: 60 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -243,7 +243,7 @@ class My2faNotificationHandler extends Sharp2faNotificationHandler // or Sharp2f
243
243
244
244
## Forgotten password
245
245
246
-
You can activate the classic Laravel Breeze workflow of forgotten password with a simple config:
246
+
You can activate the classic Laravel workflow of forgotten password with a simple config:
247
247
248
248
```php
249
249
class SharpServiceProvider extends SharpAppServiceProvider
@@ -306,9 +306,66 @@ class SharpServiceProvider extends SharpAppServiceProvider
306
306
307
307
These customizations will not interfere with any default behavior that you may have implemented for your app, outside Sharp.
308
308
309
+
## Allow the current user to change his password
310
+
311
+
Sharp provides a helper trait to quickly build a command that lets the currently authenticated user change his password: `Code16\Sharp\Auth\Password\Command\IsChangePasswordCommandTrait`. Using this trait, you can quickly build a Sharp command, with a few configuration options.
312
+
313
+
The trait will take care of the form, validation and rate-limiting. Note that:
314
+
315
+
- This helper is designed for the “current user changes his own password” scenario. If you need admin-managed password resets for other users, implement a different command with the proper authorization checks.
316
+
- Persisting the new password is up to you (see example below).
317
+
318
+
### Configuration and behavior
319
+
320
+
You can configure the behavior of the command with the following methods (should be called in your `buildCommandConfig()` method):
321
+
322
+
-`configureConfirmPassword(?bool $confirm = true)`: (false by default) enable password confirmation.
323
+
-`configurePasswordRule(Password $rule)`: (default: `Password::min(8)`) change the default password validation rule.
324
+
-`configureValidateCurrentPassword(?bool $validate = true)`: (true by default) if true, a `password` field that uses Laravel’s `current_password` rule (which compares against the currently authenticated user’s stored password) is added. Make sure you use Eloquent, and that your `User` model stores a hashed password as usual.
325
+
326
+
### Full example
327
+
328
+
```php
329
+
use Code16\Sharp\Auth\Password\Command\IsChangePasswordCommandTrait;
330
+
// ...
331
+
332
+
class ChangePasswordCommand extends SingleInstanceCommand
333
+
{
334
+
use IsChangePasswordCommandTrait;
335
+
336
+
public function buildCommandConfig(): void
337
+
{
338
+
$this->configureConfirmPassword()
339
+
->configurePasswordRule(
340
+
Password::min(8)
341
+
->numbers()
342
+
->symbols()
343
+
->uncompromised()
344
+
);
345
+
}
346
+
347
+
protected function executeSingle(array $data): array
348
+
{
349
+
// The trait handles validation and rate limiting.
350
+
351
+
auth()->user()->update([
352
+
'password' => $data['new_password'], // Considering hashing is done by the model (cast)
353
+
]);
354
+
355
+
$this->notify('Password updated!');
356
+
357
+
return $this->reload();
358
+
}
359
+
}
360
+
```
361
+
362
+
::: info
363
+
In this example we chose to create a `SingleInstanceCommand`, since it’s a common use-case to attach such a command to a "Profile" single Show Page that could be [placed in the user menu](building-menu.md#add-links-in-the-user-profile-menu), but you can decide to create an `EntityCommand` or even an `InstanceCommand` instead.
364
+
:::
365
+
309
366
## User impersonation (dev only)
310
367
311
-
At the development stage, it can be useful to replace the login form by a user impersonation. Sharp allows to do that out of the box:
368
+
At the development stage, it can be useful to replace the login form by a user impersonation. Sharp allows you to do that out of the box:
312
369
313
370
```php
314
371
class SharpServiceProvider extends SharpAppServiceProvider
@@ -392,3 +449,4 @@ class SharpServiceProvider extends SharpAppServiceProvider
0 commit comments