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

Commit ddc0a8d

Browse files
authored
Update README.md
1 parent 57a7523 commit ddc0a8d

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,23 @@ Optionally, you might want to add `'mobile'` to your `$fillable` array.
5050
## Changes to the Login Process
5151
The following two-factor authentication routes will be added automatically:
5252
```php
53-
$router->get('/auth/token', 'App\Http\Controllers\Auth\TwoFactorAuthController@showTwoFactorForm')->name('auth.token');
54-
$router->post('/auth/token', 'App\Http\Controllers\Auth\TwoFactorAuthController@verifyToken');
53+
$router->group([
54+
'middleware' => ['web', 'guest'],
55+
'namespace' => 'App\Http\Controllers\Auth',
56+
], function () use ($router) {
57+
$router->get('/auth/token', 'TwoFactorAuthController@showTwoFactorForm')->name('auth.token');
58+
$router->post('/auth/token', 'TwoFactorAuthController@verifyToken');
59+
});
5560
```
5661
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.
5762

58-
1 Add the following trait to `LoginController`:
63+
1 Add the following import to `LoginController`:
5964
```php
6065
...
61-
use MichaelDzjap\TwoFactorAuth\Http\Controllers\InitiatesTwoFactorAuthProcess;
66+
use MichaelDzjap\TwoFactorAuth\Contracts\TwoFactorProvider;
6267

6368
class LoginController extends Controller
6469
{
65-
use AuthenticatesUsers, InitiatesTwoFactorAuthProcess;
6670
...
6771
```
6872
and also add the following functions:
@@ -76,11 +80,35 @@ and also add the following functions:
7680
*/
7781
protected function authenticated(Request $request, $user)
7882
{
79-
self::shouldTwoFactorAuthenticate($request, $user);
83+
if (resolve(TwoFactorProvider::class)->enabled($user)) {
84+
return self::startTwoFactorAuthProcess($request, $user);
85+
}
86+
87+
return redirect()->intended($this->redirectPath());
8088
}
8189
```
8290
and
8391
```php
92+
/**
93+
* Log out the user and start the two factor authentication state.
94+
*
95+
* @param Request $request
96+
* @param User $user
97+
* @return \Illuminate\Http\Response
98+
*/
99+
private function startTwoFactorAuthProcess(Request $request, $user)
100+
{
101+
// Logout user, but remember user id
102+
auth()->logout();
103+
$request->session()->put('two-factor:auth:id', $user->id);
104+
105+
self::registerUserAndSendToken($user);
106+
107+
return redirect()->route('auth.token');
108+
}
109+
```
110+
and lastly
111+
```php
84112
/**
85113
* Provider specific two-factor authentication logic. In the case of MessageBird
86114
* we just want to send an authentication token via SMS.
@@ -97,7 +125,7 @@ private function registerUserAndSendToken(User $user)
97125
dispatch(new SendSMSToken($user));
98126
}
99127
```
100-
The body of the second function can be left empty 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(s) and controller method(s) yourself. The best place for this would be the `TwoFactorAuthController` to be discussed next.
128+
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.
101129

102130
2 Add a `TwoFactorAuthController` in `app/Http/Controllers/Auth` with the following content:
103131
```php

0 commit comments

Comments
 (0)