@@ -19,14 +19,19 @@ class TwoFactorAuthenticationController extends Controller implements TwoFactorA
1919 /**
2020 * User Model.
2121 */
22- protected $ userModel ;
22+ protected $ TwoFAModel ;
2323
2424 /**
2525 * Assigns $usersModel Property a Model instance.
2626 */
2727 public function __construct ()
2828 {
29- $ this ->userModel = TwoFactorAuthenticationServiceProvider::getUserModelInstance ();
29+ $ this ->TwoFAModel = TwoFactorAuthenticationServiceProvider::getTwoFAModelInstance ();
30+
31+ $ this ->middleware (function ($ request , $ next ) {
32+ $ this ->setUser (auth ()->user ());
33+ return $ next ($ request );
34+ });
3035 }
3136
3237 /**
@@ -39,14 +44,8 @@ public function __construct()
3944 */
4045 public function setupTwoFactorAuthentication (Request $ request )
4146 {
42- $ user = $ this ->userModel ->find ($ request ->user ()->id );
43- if (!Schema::hasColumn (config ('2fa-config.table ' ), 'two_factor_secret_key ' ) ||
44- !Schema::hasColumn (config ('2fa-config.table ' ), 'is_two_factor_enabled ' )) {
45- throw TwoFactorAuthenticationExceptions::columnNotFound ();
46- }
47- $ user ->two_factor_secret_key = $ user ->two_factor_secret_key ?? $ this ->base32EncodedString (config ('2fa-config.number_of_digits ' ));
48- $ user ->update ();
49-
47+ $ this ->updateUserWith2FAGeneratedKey ();
48+ $ user = $ this ->getUser ();
5049 $ totp = new TOTP (
5150 config ('2fa-config.account_name ' ),
5251 $ user ->two_factor_secret_key ,
@@ -74,7 +73,7 @@ public function setupTwoFactorAuthentication(Request $request)
7473 */
7574 public function enableTwoFactorAuthentication (Request $ request )
7675 {
77- $ user = $ this ->userModel ->find ($ request ->user ()->id );
76+ $ user = $ this ->TwoFAModel ->find ($ request ->user ()->id );
7877 $ user ->is_two_factor_enabled = 1 ;
7978 $ user ->update ();
8079
@@ -99,7 +98,7 @@ public function enableTwoFactorAuthentication(Request $request)
9998 */
10099 public function disableTwoFactorAuthentication (Request $ request )
101100 {
102- $ user = $ this ->userModel ->find ($ request ->user ()->id );
101+ $ user = $ this ->TwoFAModel ->find ($ request ->user ()->id );
103102 $ user ->is_two_factor_enabled = 0 ;
104103 $ user ->two_factor_secret_key = null ;
105104 $ user ->update ();
@@ -134,7 +133,7 @@ public function verifyTwoFactorAuthentication(Request $request)
134133 return view ('2fa::verify ' );
135134 }
136135
137- return redirect ()->back (); //shoud be configurabel
136+ return redirect ()->back (); //shoud be configurable
138137 }
139138
140139 /**
@@ -172,4 +171,15 @@ private function strRandom($length = 30):
172171
173172 return $ string ;
174173 }
174+
175+ private function updateUserWith2FAGeneratedKey ()
176+ {
177+ $ user = $ this ->TwoFAModel ->find ($ this ->getUser ()->id );
178+ if (!Schema::hasColumn (config ('2fa-config.table ' ), 'two_factor_secret_key ' ) ||
179+ !Schema::hasColumn (config ('2fa-config.table ' ), 'is_two_factor_enabled ' )) {
180+ throw TwoFactorAuthenticationExceptions::columnNotFound ();
181+ }
182+ $ user ->two_factor_secret_key = $ user ->two_factor_secret_key ?? $ this ->base32EncodedString (config ('2fa-config.number_of_digits ' ));
183+ $ user ->update ();
184+ }
175185}
0 commit comments