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
+56-2Lines changed: 56 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,62 @@ 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 trat 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)`: enable password confirmation (false by default)
323
+
-`configurePasswordRule(Password $rule)`: change the default password validation rule (default: `Password::min(8)`)
324
+
-`configureValidateCurrentPassword(?bool $validate = true)`: 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 your `User` model stores a hashed password as usual. (true by default)
325
+
326
+
### 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
+
309
362
## User impersonation (dev only)
310
363
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:
364
+
At the development stage, it can be useful to replace the login form by a user impersonation. Sharp allows doing that out of the box:
312
365
313
366
```php
314
367
class SharpServiceProvider extends SharpAppServiceProvider
@@ -392,3 +445,4 @@ class SharpServiceProvider extends SharpAppServiceProvider
0 commit comments