Skip to content

Commit 82af1f1

Browse files
committed
add optional method parameter when confirming 2fa
1 parent a42b587 commit 82af1f1

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ class TwoFactorAuthenticationController extends Controller
7878
}
7979
```
8080

81-
After enabling two-factor authentication,
82-
the user must still "confirm" their two-factor authentication configuration by providing a valid two-factor authentication code. You should provide a way for the user to do this. For example, you could provide a view that displays the QR code and secret key for the user to scan into their authenticator app:
81+
### Confirming Two-Factor Authentication
82+
83+
After enabling two-factor authentication, the user must still "confirm" their two-factor authentication configuration by providing a valid two-factor authentication code. You should provide a way for the user to do this. For example, you could provide a view that displays the QR code and secret key for the user to scan into their authenticator app:
8384
```php
8485
use Illuminate\Http\RedirectResponse;
8586
use Illuminate\Http\Request;
@@ -135,6 +136,10 @@ $user->getCurrentOtp();
135136
> **Note**
136137
> When sending the one-time-password via SMS/email, you should set the window config to a higher value, to allow the user to enter the one-time password after it has been sent.
137138
139+
The `confirmTwoFactorAuthentication` method takes an optional second parameter to specify the two-factor method, this is totally optional, it can be useful if you have multiple methods for receiving the one-time password.
140+
141+
### Disabling Two-Factor Authentication
142+
138143
You should also provide a way for the user to disable two-factor authentication. This can be done by calling the `disableTwoFactorAuthentication` method on the user model:
139144

140145
```php

database/migrations/add_two_factor_columns_to_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up(): void
1616
$table->text('two_factor_secret')->nullable();
1717
$table->text('two_factor_recovery_codes')->nullable();
1818
$table->timestamp('two_factor_confirmed_at')->nullable();
19+
$table->string('two_factor_method')->nullable();
1920
});
2021
});
2122
}

src/Actions/ConfirmTwoFactorAuthentication.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(protected TwoFactorProvider $provider)
2222
*
2323
* @param \App\Models\User $user
2424
*/
25-
public function __invoke($user, string $code): void
25+
public function __invoke($user, string $code, ?string $method): void
2626
{
2727
if (empty($user->two_factor_secret)
2828
|| empty($code)
@@ -35,6 +35,7 @@ public function __invoke($user, string $code): void
3535

3636
$user->forceFill([
3737
'two_factor_confirmed_at' => now(),
38+
'two_factor_method' => $method,
3839
])->save();
3940

4041
TwoFactorAuthenticationConfirmed::dispatch($user);

src/Actions/DisableTwoFactorAuthentication.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ class DisableTwoFactorAuthentication
1313
*/
1414
public function __invoke($user): void
1515
{
16-
if (! is_null($user->two_factor_secret) ||
17-
! is_null($user->two_factor_recovery_codes) ||
18-
! is_null($user->two_factor_confirmed_at)) {
16+
if (
17+
! is_null($user->two_factor_secret)
18+
|| ! is_null($user->two_factor_recovery_codes)
19+
|| ! is_null($user->two_factor_confirmed_at)
20+
|| ! is_null($user->two_factor_method)
21+
) {
1922
$user->forceFill([
2023
'two_factor_secret' => null,
2124
'two_factor_recovery_codes' => null,
2225
'two_factor_confirmed_at' => null,
26+
'two_factor_method' => null,
2327
])->save();
2428

2529
TwoFactorAuthenticationDisabled::dispatch($user);

src/TwoFactorAuthenticatable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ public function disableTwoFactorAuthentication(): void
103103
/**
104104
* Confirm two-factor authentication for the user.
105105
*/
106-
public function confirmTwoFactorAuthentication(string $code): void
106+
public function confirmTwoFactorAuthentication(string $code, ?string $method): void
107107
{
108-
app(ConfirmTwoFactorAuthentication::class)($this, $code);
108+
app(ConfirmTwoFactorAuthentication::class)($this, $code, $method);
109109
}
110110

111111
/**

tests/TestUser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @property \Illuminate\Support\Carbon|null $updated_at
1717
* @property string|null $two_factor_secret
1818
* @property array|null $two_factor_recovery_codes
19+
* @property string|null $two_factor_method
1920
* @property \Illuminate\Support\Carbon|null $two_factor_confirmed_at
2021
*
2122
* @method static TestUser create(array $attributes = [])

0 commit comments

Comments
 (0)