Skip to content

Commit 865ed4f

Browse files
committed
allow password confirmation customization
1 parent 3ed5e87 commit 865ed4f

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/Fortify.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class Fortify
3131
*/
3232
public static $authenticateUsingCallback;
3333

34+
/**
35+
* The callback that is responsible for confirming user passwords.
36+
*
37+
* @var callable|null
38+
*/
39+
public static $confirmPasswordsUsingCallback;
40+
3441
/**
3542
* Indicates if Fortify routes will be registered.
3643
*
@@ -197,6 +204,17 @@ public static function authenticateUsing(callable $callback)
197204
static::$authenticateUsingCallback = $callback;
198205
}
199206

207+
/**
208+
* Register a callback that is responsible for confirming existing user passwords as valid.
209+
*
210+
* @param callable $callback
211+
* @return void
212+
*/
213+
public static function confirmPasswordsUsing(callable $callback)
214+
{
215+
static::$confirmPasswordsUsingCallback = $callback;
216+
}
217+
200218
/**
201219
* Register a class / callback that should be used to create new users.
202220
*

src/Http/Controllers/ConfirmablePasswordController.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Laravel\Fortify\Contracts\ConfirmPasswordViewResponse;
99
use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse;
1010
use Laravel\Fortify\Contracts\PasswordConfirmedResponse;
11+
use Laravel\Fortify\Fortify;
1112

1213
class ConfirmablePasswordController extends Controller
1314
{
@@ -48,16 +49,26 @@ public function show(Request $request)
4849
*/
4950
public function store(Request $request)
5051
{
51-
$username = config('fortify.username');
52+
if (Fortify::$confirmPasswordsUsingCallback) {
53+
$confirmed = call_user_func(
54+
Fortify::$confirmPasswordsUsingCallback,
55+
$request->user(),
56+
$request
57+
);
58+
} else {
59+
$username = config('fortify.username');
5260

53-
if ($status = $this->guard->validate([
54-
$username => $request->user()->{$username},
55-
'password' => $request->input('password'),
56-
])) {
61+
$confirmed = $this->guard->validate([
62+
$username => $request->user()->{$username},
63+
'password' => $request->input('password')
64+
]);
65+
}
66+
67+
if ($confirmed) {
5768
$request->session()->put('auth.password_confirmed_at', time());
5869
}
5970

60-
return $status
71+
return $confirmed
6172
? app(PasswordConfirmedResponse::class)
6273
: app(FailedPasswordConfirmationResponse::class);
6374
}

tests/ConfirmablePasswordControllerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Foundation\Auth\User;
66
use Laravel\Fortify\Contracts\ConfirmPasswordViewResponse;
7+
use Laravel\Fortify\Fortify;
78

89
class ConfirmablePasswordControllerTest extends OrchestraTestCase
910
{
@@ -68,6 +69,26 @@ public function test_password_confirmation_can_fail()
6869
$this->assertNotEquals($response->getTargetUrl(), 'http://foo.com/bar');
6970
}
7071

72+
public function test_password_confirmation_can_be_customized()
73+
{
74+
Fortify::$confirmPasswordsUsingCallback = function () {
75+
return true;
76+
};
77+
78+
$response = $this->withoutExceptionHandling()
79+
->actingAs($this->user)
80+
->withSession(['url.intended' => 'http://foo.com/bar'])
81+
->post(
82+
'/user/confirm-password',
83+
['password' => 'invalid']
84+
);
85+
86+
$response->assertSessionHas('auth.password_confirmed_at');
87+
$response->assertRedirect('http://foo.com/bar');
88+
89+
Fortify::$confirmPasswordsUsingCallback = null;
90+
}
91+
7192
public function test_password_can_be_confirmed_with_json()
7293
{
7394
$response = $this->actingAs($this->user)

0 commit comments

Comments
 (0)