Skip to content

Commit 4e196df

Browse files
committed
Feature: Auto submit form when 6 digits have been entered
1 parent addc5ba commit 4e196df

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

resources/lang/en/two-factor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
'success' => 'The OTP code has been validated successfully.',
1111

12-
'fail_2fa' => 'The TOTP code has expired or is invalid.',
12+
'fail_2fa' => 'The OTP code has expired or is invalid.',
1313
'fail_confirm' => 'The OTP to activate Two-Factor Authentication is invalid.',
1414
'enabled' => 'Two-Factor Authentication has been enabled.',
1515
'disabled' => 'Two-Factor Authentication has been disabled.',
1616
'enabled_message' => 'Two-Factor Authentication was enabled on :date.',
1717

1818
'safe_device' => 'We won\'t ask you for Two-Factor Authentication codes in this device for some time.',
1919
'safe_device_hint'=>'Uncheck this if using a public or shared device.',
20-
'totp_or_recovery_code' => 'One time Pin or Recovery code',
20+
'totp_or_recovery_code' => 'One Time Pin or Recovery code',
2121
'confirm_otp_hint' => 'An OTP should have :otpLength digits, while a recovery code can be alphanumeric and should be :recoveryLength characters.',
2222
'one_time_pin' => 'One Time Pin',
2323
'enable_safe_device' => 'Remember this device for :days days.',
@@ -48,7 +48,7 @@
4848
'setup_step_1' => 'Step 1. Scan the QR Code',
4949
'setup_step_2' => 'Step 2. Enter the pin provided by your app',
5050

51-
'action_label' => 'Submit TOTP Pin and complete setup',
51+
'action_label' => 'Submit pin and complete setup',
5252

5353
'setup_message_1' => 'When two factor authentication is enabled, you will prompted for a secure pin during login. Your phone\'s authenticator application will provide a new pin every :interval seconds.',
5454
'setup_message_2' => 'To enable 2FA, scan the following QR code using an authenticator app on your phone (such as Google Authenticator, Microsoft Authenticator, Authy, or LastPass Authenticator). Then, enter the one-time passcode (OTP) to confirm the setup.',

src/Filament/Pages/Configure.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ protected function getSaveFormAction(): Action
134134
return Action::make('save')
135135
->label($this->getUser()->hasTwoFactorEnabled() ? __('filament-2fa::two-factor.save_changes') : __('filament-2fa::two-factor.action_label'))
136136
->submit('save')
137+
->visible(fn()=>!$this->getUser()->hasTwoFactorEnabled())
137138
->keyBindings(['mod+s']);
138139
}
139140

@@ -200,7 +201,7 @@ protected function getTwoFactorAuthFormComponent(): Component
200201
->relationship('twoFactorAuth')
201202
->schema([
202203
$this->enable2FactorAuthGroupComponent(),
203-
$this->disable2FactorAuthGroupComponent()
204+
$this->manage2FactorAuthGroupComponent()
204205
]);
205206
}
206207

@@ -238,13 +239,19 @@ protected function enable2FactorAuthGroupComponent(): Component
238239
->content(fn () => new HtmlString('<h3 class="text-lg font-bold text-primary">' . __('filament-2fa::two-factor.setup_step_2') . '</h3>')),
239240
TextInput::make('2fa_code')
240241
->label(__('filament-2fa::two-factor.confirm'))
241-
->numeric()
242242
->autofocus()
243243
->required(!$this->getUser()->hasTwoFactorEnabled())
244-
->minLength(config('two-factor.totp.digits'))
245-
->maxLength(config('two-factor.totp.digits'))
244+
->length(config('two-factor.totp.digits'))
246245
->autocomplete(false)
247-
->afterStateUpdated(fn ($state) => $this->data['2fa_code'] = $state),
246+
->live()
247+
->extraInputAttributes(['class'=>'text-center','style'=>'font-size:3em; letter-spacing:1rem'])
248+
->afterStateUpdated(function ($state) {
249+
$requiredLength = config('two-factor.totp.digits');
250+
if (strlen($state) == $requiredLength) {
251+
$this->data['2fa_code'] = $state;
252+
$this->save();
253+
}
254+
}),
248255
])
249256
->columnSpan([
250257
'sm' => 2, // Full-width on small screens
@@ -269,7 +276,7 @@ protected function prepareTwoFactor(): array
269276
];
270277
}
271278

272-
protected function disable2FactorAuthGroupComponent(): Component
279+
protected function manage2FactorAuthGroupComponent(): Component
273280
{
274281
return Group::make()
275282
->schema([
@@ -293,21 +300,24 @@ protected function disable2FactorAuthGroupComponent(): Component
293300
$this->recoveryCodes = $this->getUser()->generateRecoveryCodes();
294301
})
295302
->visible($this->showRecoveryCodes)
303+
->requiresConfirmation(),
304+
FormAction::make('disableTwoFactorAuth')
305+
->label(__('filament-2fa::two-factor.disable_2fa'))
306+
->color('danger')
296307
->requiresConfirmation()
308+
->icon('heroicon-m-shield-exclamation')
309+
->modalDescription('You will need to remove the account from your device. That account will not work again.')
310+
->action(function () {
311+
$this->getUser()->disableTwoFactorAuth();
312+
$this->data['disable_two_factor_auth'] = true;
313+
$this->save();
314+
}),
297315
]),
298316
Placeholder::make('recovery_code')
299317
->label('')
300318
->content($this->prepareRecoveryCodes())
301319
->visible($this->showRecoveryCodes),
302-
Toggle::make('disable_two_factor_auth')
303-
->label(__('filament-2fa::two-factor.disable_2fa'))
304-
->inline(false)
305-
->onColor('danger')
306-
->offColor('success')
307-
->onIcon('heroicon-m-x-mark')
308-
->offIcon('heroicon-m-check-circle')
309-
->live()
310-
->afterStateUpdated(fn($state) => $this->data['disable_two_factor_auth'] = $state),
320+
311321
])
312322
->visible($this->getUser()->hasTwoFactorEnabled());
313323
}

src/Livewire/Confirm2Fa.php

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ protected function get2FaFormComponent(): Component
142142
->maxLength(8)
143143
->required()
144144
->autocomplete(false)
145+
->extraInputAttributes(['class'=>'text-center','style'=>'font-size:2.6em; letter-spacing:1rem'])
145146
->live()
146147
->afterStateUpdated(function ($state) {
147148
$requiredLength = config('two-factor.totp.digits');

0 commit comments

Comments
 (0)