Skip to content

Commit b66e69e

Browse files
jasonvargaclaude
andcommitted
Stop auto-logging in users after password reset
Instead of logging users in after a password reset, redirect them to the login page (CP) or the redirect path (front-end) unauthenticated. This avoids needing special handling for 2FA and passkey-enforced login. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 01395e6 commit b66e69e

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

resources/js/pages/auth/passwords/Reset.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ const submit = () => {
3030
processing.value = true;
3131
errors.value = {};
3232
},
33-
onSuccess: (e) => {
34-
return window.location.href = e.url;
35-
},
3633
onError: () => processing.value = false
3734
});
3835
}

src/Auth/ResetsPasswords.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ protected function resetPassword($user, $password)
130130
$user->save();
131131

132132
event(new PasswordReset($user));
133-
134-
$this->guard()->login($user);
135133
}
136134

137135
/**

src/Http/Controllers/CP/Auth/ResetPasswordController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function broker()
2525
return Password::broker($broker);
2626
}
2727

28+
public function redirectPath()
29+
{
30+
return cp_route('login');
31+
}
32+
2833
protected function resetFormAction()
2934
{
3035
return route('statamic.cp.password.reset.action');

src/Http/Middleware/CP/HandleInertiaRequests.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ private function toasts(Request $request)
7575
$this->toasts->success($message);
7676
}
7777

78+
if ($message = $session->get('status')) {
79+
$this->toasts->success($message);
80+
}
81+
7882
if ($message = $session->get('error')) {
7983
$this->toasts->error($message);
8084
}

tests/Auth/ResetPasswordTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Tests\Auth;
4+
5+
use Illuminate\Support\Facades\Hash;
6+
use Illuminate\Support\Facades\Password;
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Statamic\Auth\Passwords\PasswordReset;
10+
use Statamic\Facades\User;
11+
use Tests\PreventSavingStacheItemsToDisk;
12+
use Tests\TestCase;
13+
14+
class ResetPasswordTest extends TestCase
15+
{
16+
use PreventSavingStacheItemsToDisk;
17+
18+
public static function resetPasswordProvider()
19+
{
20+
return [
21+
'cp' => ['cp'],
22+
'web' => ['web'],
23+
];
24+
}
25+
26+
private function resetUrl($type)
27+
{
28+
return match ($type) {
29+
'cp' => cp_route('password.reset.action'),
30+
'web' => route('statamic.password.reset.action'),
31+
};
32+
}
33+
34+
private function defaultRedirectUrl($type)
35+
{
36+
return match ($type) {
37+
'cp' => cp_route('login'),
38+
'web' => route('statamic.site'),
39+
};
40+
}
41+
42+
private function createUser()
43+
{
44+
return tap(User::make()->makeSuper()->email('san@holo.com')->password('secret'))->save();
45+
}
46+
47+
private function createToken($user, $type)
48+
{
49+
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);
50+
51+
if (is_array($broker)) {
52+
$broker = $broker[$type];
53+
}
54+
55+
return Password::broker($broker)->createToken($user);
56+
}
57+
58+
#[Test]
59+
#[DataProvider('resetPasswordProvider')]
60+
public function it_resets_the_password_and_user_is_not_authenticated($type)
61+
{
62+
$user = $this->createUser();
63+
$token = $this->createToken($user, $type);
64+
65+
$this
66+
->assertGuest()
67+
->post($this->resetUrl($type), [
68+
'token' => $token,
69+
'email' => 'san@holo.com',
70+
'password' => 'newpassword',
71+
'password_confirmation' => 'newpassword',
72+
])
73+
->assertSessionHas('status')
74+
->assertRedirect($this->defaultRedirectUrl($type));
75+
76+
$this->assertGuest();
77+
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
78+
}
79+
}

0 commit comments

Comments
 (0)