@@ -34,6 +34,9 @@ class TwoFactor extends Component
3434 #[Validate('required|string|size:6 ' , onUpdate: false )]
3535 public string $ code = '' ;
3636
37+ /**
38+ * Mount the component.
39+ */
3740 public function mount (DisableTwoFactorAuthentication $ disableTwoFactorAuthentication ): void
3841 {
3942 abort_unless (Features::enabled (Features::twoFactorAuthentication ()), Response::HTTP_FORBIDDEN );
@@ -46,6 +49,9 @@ public function mount(DisableTwoFactorAuthentication $disableTwoFactorAuthentica
4649 $ this ->requiresConfirmation = Features::optionEnabled (Features::twoFactorAuthentication (), 'confirm ' );
4750 }
4851
52+ /**
53+ * Enable two-factor authentication for the user.
54+ */
4955 public function enable (EnableTwoFactorAuthentication $ enableTwoFactorAuthentication ): void
5056 {
5157 $ enableTwoFactorAuthentication (auth ()->user ());
@@ -54,53 +60,36 @@ public function enable(EnableTwoFactorAuthentication $enableTwoFactorAuthenticat
5460 $ this ->twoFactorEnabled = auth ()->user ()->hasEnabledTwoFactorAuthentication ();
5561 }
5662
57- $ this ->loadTwoFactorData ();
58- $ this ->showModal = true ;
59- }
63+ $ this ->loadSetupData ();
6064
61- public function disable (DisableTwoFactorAuthentication $ disableTwoFactorAuthentication ): void
62- {
63- $ disableTwoFactorAuthentication (auth ()->user ());
64- $ this ->twoFactorEnabled = false ;
65+ $ this ->showModal = true ;
6566 }
6667
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
6872 {
69- $ this ->validate ();
70- $ confirmTwoFactorAuthentication (auth ()->user (), $ this ->code );
71- $ this ->closeModal ();
72- $ this ->twoFactorEnabled = true ;
73- }
73+ $ user = auth ()->user ();
7474
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. ' );
8480
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 ' );
9182 }
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- ];
9883 }
9984
100- public function handleNextAction (): void
85+ /**
86+ * Show the two-factor verification step if necessary.
87+ */
88+ public function showVerificationIfNecessary (): void
10189 {
10290 if ($ this ->requiresConfirmation ) {
10391 $ this ->showVerificationStep = true ;
92+
10493 $ this ->resetErrorBag ();
10594
10695 return ;
@@ -109,38 +98,85 @@ public function handleNextAction(): void
10998 $ this ->closeModal ();
11099 }
111100
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+ */
112118 public function resetVerification (): void
113119 {
114120 $ this ->reset ('code ' , 'showVerificationStep ' );
121+
115122 $ this ->resetErrorBag ();
116123 }
117124
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+ */
118138 public function closeModal (): void
119139 {
120140 $ this ->reset (
121141 'code ' ,
122- 'showVerificationStep ' ,
123142 'manualSetupKey ' ,
124143 'qrCodeSvg ' ,
125144 'showModal ' ,
145+ 'showVerificationStep ' ,
126146 );
147+
127148 $ this ->resetErrorBag ();
128149
129150 if (! $ this ->requiresConfirmation ) {
130151 $ this ->twoFactorEnabled = auth ()->user ()->hasEnabledTwoFactorAuthentication ();
131152 }
132153 }
133154
134- private function loadTwoFactorData (): void
155+ /**
156+ * Get the current modal configuration state.
157+ */
158+ public function getModalConfigProperty (): array
135159 {
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+ }
137167
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+ ] ;
144174 }
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+ ];
145181 }
146182}
0 commit comments