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
{{ message }}
This repository was archived by the owner on Apr 19, 2025. It is now read-only.
-[Using a Custom Provider](#using-a-custom-provider)
18
20
-[Errors and Exceptions](#errors-and-exceptions)
19
21
-[Testing](#testing)
@@ -30,7 +32,7 @@ From _Laravel_ 5.8 and onwards, the default is to use `bigIncrements` instead of
30
32
31
33
Publishing the package's migration files allows for more flexibility with regards to customising your database structure. However, it could also cause complications if you already have ran migrations as part of installing previous versions of this package. In this case you simply might want to bypass running the migrations again or only run them when in a specific environment. The `Schema::hasColumn()` and `Schema::hasTable()` methods should be of use here.
32
34
33
-
### Optional correction
35
+
### Optional Correction
34
36
Versions of this package prior to v2.3.0 incorrectly created the `user_id` column on the `two_factor_auths` table using `increments` instead of `unsignedInteger`. Practically speaking, this error is of no concern. Although there is no need to have a _primary_ key for the `user_id` column, it doesn't cause any problems either. However, if for some reason you don't like this idea, it is safe to remove the _primary_ key using a migration of the form
35
37
36
38
```php
@@ -216,6 +218,7 @@ The first route is the route the user will be redirected to once the two-factor
216
218
namespace App\Http\Controllers\Auth;
217
219
218
220
use App\Http\Controllers\Controller;
221
+
use App\Providers\RouteServiceProvider;
219
222
use MichaelDzjap\TwoFactorAuth\Http\Controllers\TwoFactorAuthenticatesUsers;
220
223
221
224
class TwoFactorAuthController extends Controller
@@ -241,7 +244,7 @@ The first route is the route the user will be redirected to once the two-factor
3. If you want to give textual feedback to the user when two-factor authentication fails due to an expired token or when throttling kicks in you may want to add this to `resources/views/auth/login.blade.php`:
@@ -262,6 +265,40 @@ The first route is the route the user will be redirected to once the two-factor
262
265
...
263
266
```
264
267
268
+
### Failed Verification Attempt Handling
269
+
The default behaviour is to redirect to the previous view with an error message in case token verification fails. However, there most likely are instances where you would like to handle a failed token verification attempt differently. For instance, in the case of _MessageBird_ a token can only be verified once. Any attempt with the same token after a first failed attempt will always throw a `TokenAlreadyProcessedException` and hence, it would make more sense to either redirect to the _/login_ route again to start the entire authentication process from scratch or to redirect to a view where a new token can be requested.
270
+
271
+
In order to change the default behaviour it is possible to specify either a `$redirectToAfterFailure` property or a protected `redirectToAfterFailure` method on your `TwoFactorAuthController`. If one of these is present (the method taking precedence over the property), the default behaviour is bypassed and the user will be redirected to the specified route. To give a simple example, suppose you simply want to redirect to the _/login_ route after a failed verification attempt you would structure your `TwoFactorAuthController` like:
272
+
```php
273
+
<?php
274
+
275
+
namespace App\Http\Controllers\Auth;
276
+
277
+
use App\Http\Controllers\Controller;
278
+
use App\Providers\RouteServiceProvider;
279
+
use MichaelDzjap\TwoFactorAuth\Http\Controllers\TwoFactorAuthenticatesUsers;
280
+
281
+
class TwoFactorAuthController extends Controller
282
+
{
283
+
use TwoFactorAuthenticatesUsers;
284
+
285
+
/**
286
+
* Where to redirect users after two-factor authentication passes.
* Where to redirect users after two-factor authentication fails.
294
+
*
295
+
* @var string
296
+
*/
297
+
protected $redirectToAfterFailure = '/login';
298
+
}
299
+
```
300
+
Redirecting a user to a route for generating a fresh authentication token would require a bit more work, but certainly is possible this way.
301
+
265
302
## Using a Custom Provider
266
303
Since the v2.1.0 release it is possible to user your own custom provider. To do so your provider needs to implement `MichaelDzjap\TwoFactorAuth\Contracts\TwoFactorProvider` (and possibly `MichaelDzjap\TwoFactorAuth\Contracts\SMSToken` if you want to send the authentication token via SMS).
0 commit comments