Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 26a974d

Browse files
committed
#11: Optionally redirect to a custom route after token verification failure
1 parent 6cd28fd commit 26a974d

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

src/Http/Controllers/TwoFactorAuthenticatesUsers.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,7 @@ public function verifyToken(VerifySMSToken $request)
5858
return $this->sendTwoFactorAuthResponse($request);
5959
}
6060

61-
// If the two-factor authentication attempt was unsuccessful we will increment
62-
// the number of attempts to two-factor authenticate and redirect the user
63-
// back to the two-factor authentication form. Of course, when this user
64-
// surpasses their maximum number of attempts they will get locked out.
65-
$this->incrementTwoFactorAuthAttempts($request);
66-
67-
return $this->sendFailedTwoFactorAuthResponse($request);
61+
return $this->handleFailedAttempt($request);
6862
}
6963

7064
/**
@@ -122,6 +116,49 @@ protected function authenticated(Request $request, $user)
122116
//
123117
}
124118

119+
/**
120+
* Handle the case where a user has submitted an invalid token.
121+
*
122+
* Default: If the two-factor authentication attempt was unsuccessful we
123+
* will increment the number of attempts to two-factor authenticate and
124+
* redirect the user back to the two-factor authentication form. Of course,
125+
* when this user surpasses their maximum number of attempts they will get
126+
* locked out.
127+
*
128+
* @param \Illuminate\Http\Request $request
129+
* @return \Illuminate\Http\Response
130+
*/
131+
protected function handleFailedAttempt(Request $request)
132+
{
133+
$this->incrementTwoFactorAuthAttempts($request);
134+
135+
if ($path = $this->redirectAfterFailurePath()) {
136+
return redirect()->to($path)->withErrors([
137+
'token' => __('twofactor-auth::twofactor-auth.failed')
138+
]);
139+
}
140+
141+
return $this->sendFailedTwoFactorAuthResponse($request);
142+
}
143+
144+
/**
145+
* Get the post two-factor authentication failure redirect path.
146+
*
147+
* @return null|string
148+
*/
149+
protected function redirectAfterFailurePath(): ?string
150+
{
151+
if (method_exists($this, 'redirectToAfterFailure')) {
152+
return $this->redirectToAfterFailure();
153+
}
154+
155+
if (property_exists($this, 'redirectToAfterFailure')) {
156+
return $this->redirectToAfterFailure;
157+
}
158+
159+
return null;
160+
}
161+
125162
/**
126163
* Throw a validation exception when two-factor authentication attempt fails.
127164
* NOTE: Throwing a validation exception is cleaner than redirecting, but

src/config/twofactor-auth.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@
22

33
return [
44

5-
/*
6-
|--------------------------------------------------------------------------
7-
| Enabled
8-
|--------------------------------------------------------------------------
9-
|
10-
| Options:
11-
|
12-
| - 'always': Always require two-factor authentication.
13-
| - 'never': Never require two-factor authentication.
14-
| - 'user': Specify manually for which users to enable 2fa.
15-
|
16-
*/
17-
18-
'enabled' => 'user',
19-
205
/*
216
|--------------------------------------------------------------------------
227
| Default Two-Factor Authentication Provider
@@ -61,6 +46,21 @@
6146

6247
],
6348

49+
/*
50+
|--------------------------------------------------------------------------
51+
| Enabled Mode
52+
|--------------------------------------------------------------------------
53+
|
54+
| Options:
55+
|
56+
| 'always': Always require two-factor authentication.
57+
| 'never': Never require two-factor authentication.
58+
| 'user': Specify manually for which users to enable 2fa.
59+
|
60+
*/
61+
62+
'enabled' => 'user',
63+
6464
/*
6565
|--------------------------------------------------------------------------
6666
| Routes Configuration + Naming

0 commit comments

Comments
 (0)