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.
Copy file name to clipboardExpand all lines: README.md
+30-8Lines changed: 30 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ This is a two-factor authentication package for *Laravel*. It is heavily inspire
9
9
- The current version of this package is only guaranteed to work with Laravel >= 5.5. Version 1.* of this package works with Laravel 5.4. Versions of Laravel prior to 5.4 have not been tested.
and don't forget to add your `MESSAGEBIRD_ACCESS_KEY` and `TWO_FACTOR_AUTH_DRIVER=messagebird` variables to the `.env`. If you instead wish to use the `'null'` driver (default) then do **NOT** define the `TWO_FACTOR_AUTH_DRIVER` variable in your `.env`.
21
21
22
-
2 Add the service provider to the `'providers'` array in `config/app.php`:
22
+
2. Add the service provider to the `'providers'` array in `config/app.php`:
3 Run the following *artisan* command to publish the configuration, language and view files:
26
+
3. Run the following *artisan* command to publish the configuration, language and view files:
27
27
```
28
28
php artisan vendor:publish
29
29
```
30
30
If you want to publish only one of these file groups, for instance if you don't need the views or language files, you can append one of the following commands to the *artisan* command: `--tag=config`, `--tag=lang` or `--tag-views`.
31
31
32
-
4 Run the following *artisan* command to run the database migrations
32
+
4. Run the following *artisan* command to run the database migrations
33
33
```
34
34
php artisan migrate
35
35
```
36
36
This will add a `mobile` column to the `users` table and create a `two_factor_auths` table.
37
37
38
-
5 Add the following trait to your `User` model:
38
+
5. Add the following trait to your `User` model:
39
39
```php
40
40
...
41
41
use MichaelDzjap\TwoFactorAuth\TwoFactorAuthenticable;
@@ -60,7 +60,7 @@ $router->group([
60
60
```
61
61
The first route is the route the user will be redirected to once the two-factor authentication process has been initiated. The second route is used to verify the two-factor authentication token that is to be entered by the user. The `showTwoFactorForm` controller method does exactly what it says. There do exist cases where you might want to respond differently however. For instance, instead of loading a view you might just want to return a `json` response. In that case you can simply overwrite `showTwoFactorForm` in the `TwoFactorAuthController` to be discussed below.
62
62
63
-
1 Add the following import to `LoginController`:
63
+
1. Add the following import to `LoginController`:
64
64
```php
65
65
...
66
66
use MichaelDzjap\TwoFactorAuth\Contracts\TwoFactorProvider;
@@ -129,7 +129,7 @@ private function registerUserAndSendToken(User $user)
129
129
```
130
130
You can discard the third function if you do not want to send a two-factor authentication token automatically after a successful login attempt. Instead, you might want the user to instantiate this process from the form him/herself. In that case you would have to add the required route and controller method to trigger this function yourself. The best place for this would be the `TwoFactorAuthController` to be discussed next.
131
131
132
-
2 Add a `TwoFactorAuthController` in `app/Http/Controllers/Auth` with the following content:
132
+
2. Add a `TwoFactorAuthController` in `app/Http/Controllers/Auth` with the following content:
133
133
```php
134
134
<?php
135
135
@@ -164,7 +164,7 @@ class TwoFactorAuthController extends Controller
164
164
protected $redirectTo = '/home';
165
165
}
166
166
```
167
-
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`:
167
+
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`:
@@ -183,5 +183,27 @@ class TwoFactorAuthController extends Controller
183
183
...
184
184
```
185
185
186
+
## Using a Custom Provider
187
+
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).
188
+
189
+
1. Assuming the name of your custom provider is 'dummy', you should register it with `TwoFactorAuthManager` from a service provider (could be `\App\Providers\AppServiceProvider`):
190
+
```php
191
+
resolve(\MichaelDzjap\TwoFactorAuth\TwoFactorAuthManager)->extend('dummy', function ($app) {
192
+
return new DummyProvider;
193
+
});
194
+
```
195
+
2. Add an entry for you custom provider in the 'provider' array in *app/config/twofactor-auth.php*:
196
+
```php
197
+
...
198
+
'dummy' => [
199
+
'driver' => 'dummy',
200
+
],
201
+
...
202
+
```
203
+
3. Lastly, don't forget to change the name of the provider in your *.env*:
204
+
```
205
+
TWO_FACTOR_AUTH_DRIVER=dummy
206
+
```
207
+
186
208
## Errors and Exceptions
187
209
Unfortunately the *MessageBird* php api throws rather generic exceptions when the verification of a token fails. The only way to distinguish an expired token from an invalid token is by comparing their error messages, which obviously is not a very robust mechanism. The reason this is rather unfortunate is because in the case of an invalid token we want to give the user at least a few (3) changes to re-enter the token before throttling kicks in, whereas in the case of an expired token we just want to redirect to the login screen right away.
0 commit comments