File tree Expand file tree Collapse file tree 3 files changed +56
-6
lines changed Expand file tree Collapse file tree 3 files changed +56
-6
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,13 @@ class Fortify
31
31
*/
32
32
public static $ authenticateUsingCallback ;
33
33
34
+ /**
35
+ * The callback that is responsible for confirming user passwords.
36
+ *
37
+ * @var callable|null
38
+ */
39
+ public static $ confirmPasswordsUsingCallback ;
40
+
34
41
/**
35
42
* Indicates if Fortify routes will be registered.
36
43
*
@@ -197,6 +204,17 @@ public static function authenticateUsing(callable $callback)
197
204
static ::$ authenticateUsingCallback = $ callback ;
198
205
}
199
206
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
+
200
218
/**
201
219
* Register a class / callback that should be used to create new users.
202
220
*
Original file line number Diff line number Diff line change 8
8
use Laravel \Fortify \Contracts \ConfirmPasswordViewResponse ;
9
9
use Laravel \Fortify \Contracts \FailedPasswordConfirmationResponse ;
10
10
use Laravel \Fortify \Contracts \PasswordConfirmedResponse ;
11
+ use Laravel \Fortify \Fortify ;
11
12
12
13
class ConfirmablePasswordController extends Controller
13
14
{
@@ -48,16 +49,26 @@ public function show(Request $request)
48
49
*/
49
50
public function store (Request $ request )
50
51
{
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 ' );
52
60
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 ) {
57
68
$ request ->session ()->put ('auth.password_confirmed_at ' , time ());
58
69
}
59
70
60
- return $ status
71
+ return $ confirmed
61
72
? app (PasswordConfirmedResponse::class)
62
73
: app (FailedPasswordConfirmationResponse::class);
63
74
}
Original file line number Diff line number Diff line change 4
4
5
5
use Illuminate \Foundation \Auth \User ;
6
6
use Laravel \Fortify \Contracts \ConfirmPasswordViewResponse ;
7
+ use Laravel \Fortify \Fortify ;
7
8
8
9
class ConfirmablePasswordControllerTest extends OrchestraTestCase
9
10
{
@@ -68,6 +69,26 @@ public function test_password_confirmation_can_fail()
68
69
$ this ->assertNotEquals ($ response ->getTargetUrl (), 'http://foo.com/bar ' );
69
70
}
70
71
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
+
71
92
public function test_password_can_be_confirmed_with_json ()
72
93
{
73
94
$ response = $ this ->actingAs ($ this ->user )
You can’t perform that action at this time.
0 commit comments