@@ -34,6 +34,9 @@ class TwoFactor extends Component
34
34
#[Validate('required|string|size:6 ' , onUpdate: false )]
35
35
public string $ code = '' ;
36
36
37
+ /**
38
+ * Mount the component.
39
+ */
37
40
public function mount (DisableTwoFactorAuthentication $ disableTwoFactorAuthentication ): void
38
41
{
39
42
abort_unless (Features::enabled (Features::twoFactorAuthentication ()), Response::HTTP_FORBIDDEN );
@@ -46,6 +49,9 @@ public function mount(DisableTwoFactorAuthentication $disableTwoFactorAuthentica
46
49
$ this ->requiresConfirmation = Features::optionEnabled (Features::twoFactorAuthentication (), 'confirm ' );
47
50
}
48
51
52
+ /**
53
+ * Enable two-factor authentication for the user.
54
+ */
49
55
public function enable (EnableTwoFactorAuthentication $ enableTwoFactorAuthentication ): void
50
56
{
51
57
$ enableTwoFactorAuthentication (auth ()->user ());
@@ -54,53 +60,36 @@ public function enable(EnableTwoFactorAuthentication $enableTwoFactorAuthenticat
54
60
$ this ->twoFactorEnabled = auth ()->user ()->hasEnabledTwoFactorAuthentication ();
55
61
}
56
62
57
- $ this ->loadTwoFactorData ();
58
- $ this ->showModal = true ;
59
- }
63
+ $ this ->loadSetupData ();
60
64
61
- public function disable (DisableTwoFactorAuthentication $ disableTwoFactorAuthentication ): void
62
- {
63
- $ disableTwoFactorAuthentication (auth ()->user ());
64
- $ this ->twoFactorEnabled = false ;
65
+ $ this ->showModal = true ;
65
66
}
66
67
67
- public function confirmTwoFactor (ConfirmTwoFactorAuthentication $ confirmTwoFactorAuthentication ): void
68
+ /**
69
+ * Load the two-factor authentication setup data for the user.
70
+ */
71
+ private function loadSetupData (): void
68
72
{
69
- $ this ->validate ();
70
- $ confirmTwoFactorAuthentication (auth ()->user (), $ this ->code );
71
- $ this ->closeModal ();
72
- $ this ->twoFactorEnabled = true ;
73
- }
73
+ $ user = auth ()->user ();
74
74
75
- public function getModalConfigProperty (): array
76
- {
77
- if ($ this ->twoFactorEnabled ) {
78
- return [
79
- 'title ' => __ ('Two-Factor Authentication Enabled ' ),
80
- 'description ' => __ ('Two-factor authentication is now enabled. Scan the QR code or enter the setup key in your authenticator app. ' ),
81
- 'buttonText ' => __ ('Close ' ),
82
- ];
83
- }
75
+ try {
76
+ $ this ->qrCodeSvg = $ user ?->twoFactorQrCodeSvg();
77
+ $ this ->manualSetupKey = decrypt ($ user ->two_factor_secret );
78
+ } catch (Exception ) {
79
+ $ this ->addError ('setupData ' , 'Failed to fetch setup data. ' );
84
80
85
- if ($ this ->showVerificationStep ) {
86
- return [
87
- 'title ' => __ ('Verify Authentication Code ' ),
88
- 'description ' => __ ('Enter the 6-digit code from your authenticator app ' ),
89
- 'buttonText ' => __ ('Continue ' ),
90
- ];
81
+ $ this ->reset ('qrCodeSvg ' , 'manualSetupKey ' );
91
82
}
92
-
93
- return [
94
- 'title ' => __ ('Enable Two-Factor Authentication ' ),
95
- 'description ' => __ ('To finish enabling two-factor authentication, scan the QR code or enter the setup key in your authenticator app ' ),
96
- 'buttonText ' => __ ('Continue ' ),
97
- ];
98
83
}
99
84
100
- public function handleNextAction (): void
85
+ /**
86
+ * Show the two-factor verification step if necessary.
87
+ */
88
+ public function showVerificationIfNecessary (): void
101
89
{
102
90
if ($ this ->requiresConfirmation ) {
103
91
$ this ->showVerificationStep = true ;
92
+
104
93
$ this ->resetErrorBag ();
105
94
106
95
return ;
@@ -109,38 +98,85 @@ public function handleNextAction(): void
109
98
$ this ->closeModal ();
110
99
}
111
100
101
+ /**
102
+ * Confirm two-factor authentication for the user.
103
+ */
104
+ public function confirmTwoFactor (ConfirmTwoFactorAuthentication $ confirmTwoFactorAuthentication ): void
105
+ {
106
+ $ this ->validate ();
107
+
108
+ $ confirmTwoFactorAuthentication (auth ()->user (), $ this ->code );
109
+
110
+ $ this ->closeModal ();
111
+
112
+ $ this ->twoFactorEnabled = true ;
113
+ }
114
+
115
+ /**
116
+ * Reset two-factor verification state.
117
+ */
112
118
public function resetVerification (): void
113
119
{
114
120
$ this ->reset ('code ' , 'showVerificationStep ' );
121
+
115
122
$ this ->resetErrorBag ();
116
123
}
117
124
125
+ /**
126
+ * Disable two-factor authentication for the user.
127
+ */
128
+ public function disable (DisableTwoFactorAuthentication $ disableTwoFactorAuthentication ): void
129
+ {
130
+ $ disableTwoFactorAuthentication (auth ()->user ());
131
+
132
+ $ this ->twoFactorEnabled = false ;
133
+ }
134
+
135
+ /**
136
+ * Close the two-factor authentication modal.
137
+ */
118
138
public function closeModal (): void
119
139
{
120
140
$ this ->reset (
121
141
'code ' ,
122
- 'showVerificationStep ' ,
123
142
'manualSetupKey ' ,
124
143
'qrCodeSvg ' ,
125
144
'showModal ' ,
145
+ 'showVerificationStep ' ,
126
146
);
147
+
127
148
$ this ->resetErrorBag ();
128
149
129
150
if (! $ this ->requiresConfirmation ) {
130
151
$ this ->twoFactorEnabled = auth ()->user ()->hasEnabledTwoFactorAuthentication ();
131
152
}
132
153
}
133
154
134
- private function loadTwoFactorData (): void
155
+ /**
156
+ * Get the current modal configuration state.
157
+ */
158
+ public function getModalConfigProperty (): array
135
159
{
136
- $ user = auth ()->user ();
160
+ if ($ this ->twoFactorEnabled ) {
161
+ return [
162
+ 'title ' => __ ('Two-Factor Authentication Enabled ' ),
163
+ 'description ' => __ ('Two-factor authentication is now enabled. Scan the QR code or enter the setup key in your authenticator app. ' ),
164
+ 'buttonText ' => __ ('Close ' ),
165
+ ];
166
+ }
137
167
138
- try {
139
- $ this -> qrCodeSvg = $ user ?->twoFactorQrCodeSvg();
140
- $ this -> manualSetupKey = decrypt ( $ user -> two_factor_secret );
141
- } catch ( Exception ) {
142
- $ this -> addError ( ' setupData ' , ' Failed to fetch setup data. ' );
143
- $ this -> reset ( ' qrCodeSvg ' , ' manualSetupKey ' ) ;
168
+ if ( $ this -> showVerificationStep ) {
169
+ return [
170
+ ' title ' => __ ( ' Verify Authentication Code ' ),
171
+ ' description ' => __ ( ' Enter the 6-digit code from your authenticator app. ' ),
172
+ ' buttonText ' => __ ( ' Continue ' ),
173
+ ] ;
144
174
}
175
+
176
+ return [
177
+ 'title ' => __ ('Enable Two-Factor Authentication ' ),
178
+ 'description ' => __ ('To finish enabling two-factor authentication, scan the QR code or enter the setup key in your authenticator app. ' ),
179
+ 'buttonText ' => __ ('Continue ' ),
180
+ ];
145
181
}
146
182
}
0 commit comments