@@ -21,10 +21,7 @@ class TwoFactorAuthenticatedSessionController extends Controller
2121 */
2222 public function create (Request $ request )
2323 {
24- if (! $ request ->session ()->has ('login.id ' )) {
25- return redirect ()->route ('login ' );
26- }
27-
24+ // Session check is now handled by the EnsureTwoFactorChallengeSession middleware
2825 return Inertia::render ('auth/two-factor-challenge ' );
2926 }
3027
@@ -41,49 +38,75 @@ public function store(Request $request)
4138 'recovery_code ' => 'nullable|string ' ,
4239 ]);
4340
44- $ userId = $ request ->session ()->get ('login.id ' );
45- $ user = User::find ($ userId );
46-
47- if (! $ user ) {
48- return redirect ()->route ('login ' );
49- }
41+ // If we made it here, user is available via the EnsureTwoFactorChallengeSession middleware
42+ $ user = $ request ->two_factor_auth_user ;
5043
51- // Handle TOTP code
44+ // Handle one-time password (OTP) code
5245 if ($ request ->filled ('code ' )) {
53- $ secret = decrypt ($ user ->two_factor_secret );
54- $ valid = app (\App \Actions \TwoFactorAuth \VerifyTwoFactorCode::class)($ secret , $ request ->code );
55- if ($ valid ) {
56- app (CompleteTwoFactorAuthentication::class)($ user );
57- return redirect ()->intended (route ('dashboard ' , absolute: false ));
58- }
59- return back ()->withErrors (['code ' => __ ('The provided two factor authentication code was invalid. ' )]);
46+ return $ this ->authenticateUsingCode ($ request , $ user );
6047 }
6148
6249 // Handle recovery code
6350 if ($ request ->filled ('recovery_code ' )) {
64- $ recoveryCodes = json_decode (decrypt ($ user ->two_factor_recovery_codes ), true );
65- $ provided = $ request ->recovery_code ;
66- $ match = collect ($ recoveryCodes )->first (function ($ code ) use ($ provided ) {
67- return hash_equals ($ code , $ provided );
68- });
69- if (! $ match ) {
70- return back ()->withErrors (['recovery_code ' => __ ('The provided two factor authentication recovery code was invalid. ' )]);
71- }
72- // Remove used recovery code using the ProcessRecoveryCode action
73- $ updatedCodes = app (ProcessRecoveryCode::class)($ recoveryCodes , $ match );
74- if ($ updatedCodes === false ) {
75- return back ()->withErrors (['recovery_code ' => __ ('The provided two factor authentication recovery code was invalid. ' )]);
76- }
77- $ user ->two_factor_recovery_codes = encrypt (json_encode ($ updatedCodes ));
78- $ user ->save ();
79- // Complete the authentication process
51+ return $ this ->authenticateUsingRecoveryCode ($ request , $ user );
52+ }
53+
54+ return back ()->withErrors (['code ' => __ ('Please provide a valid two factor code. ' )]);
55+ }
56+
57+ /**
58+ * Authenticate using a one-time password (OTP).
59+ *
60+ * @param \Illuminate\Http\Request $request
61+ * @param \App\Models\User $user
62+ * @return \Illuminate\Http\Response
63+ */
64+ protected function authenticateUsingCode (Request $ request , User $ user )
65+ {
66+ $ secret = decrypt ($ user ->two_factor_secret );
67+ $ valid = app (\App \Actions \TwoFactorAuth \VerifyTwoFactorCode::class)($ secret , $ request ->code );
68+
69+ if ($ valid ) {
8070 app (CompleteTwoFactorAuthentication::class)($ user );
81-
82- // Redirect to the intended page
8371 return redirect ()->intended (route ('dashboard ' , absolute: false ));
8472 }
73+
74+ return back ()->withErrors (['code ' => __ ('The provided two factor authentication code was invalid. ' )]);
75+ }
8576
86- return back ()->withErrors (['code ' => __ ('Please provide a valid two factor authentication code. ' )]);
77+ /**
78+ * Authenticate using a recovery code.
79+ *
80+ * @param \Illuminate\Http\Request $request
81+ * @param \App\Models\User $user
82+ * @return \Illuminate\Http\Response
83+ */
84+ protected function authenticateUsingRecoveryCode (Request $ request , User $ user )
85+ {
86+ $ recoveryCodes = json_decode (decrypt ($ user ->two_factor_recovery_codes ), true );
87+ $ provided = $ request ->recovery_code ;
88+ $ match = collect ($ recoveryCodes )->first (function ($ code ) use ($ provided ) {
89+ return hash_equals ($ code , $ provided );
90+ });
91+
92+ if (! $ match ) {
93+ return back ()->withErrors (['recovery_code ' => __ ('The provided two factor authentication recovery code was invalid. ' )]);
94+ }
95+
96+ // Remove used recovery code using the ProcessRecoveryCode action
97+ $ updatedCodes = app (ProcessRecoveryCode::class)($ recoveryCodes , $ match );
98+ if ($ updatedCodes === false ) {
99+ return back ()->withErrors (['recovery_code ' => __ ('The provided two factor authentication recovery code was invalid. ' )]);
100+ }
101+
102+ $ user ->two_factor_recovery_codes = encrypt (json_encode ($ updatedCodes ));
103+ $ user ->save ();
104+
105+ // Complete the authentication process
106+ app (CompleteTwoFactorAuthentication::class)($ user );
107+
108+ // Redirect to the intended page
109+ return redirect ()->intended (route ('dashboard ' , absolute: false ));
87110 }
88111}
89112
0 commit comments