File tree Expand file tree Collapse file tree 6 files changed +83
-34
lines changed
tests/Unit/Libraries/Auth/Adapters Expand file tree Collapse file tree 6 files changed +83
-34
lines changed Original file line number Diff line number Diff line change @@ -110,20 +110,6 @@ public function user(): ?User
110110 }
111111 }
112112
113- /**
114- * Verify OTP
115- * @param int $otp
116- * @param string $otpToken
117- * @return array
118- * @throws AuthException
119- * @throws JwtException
120- */
121- public function verifyOtp (int $ otp , string $ otpToken ): array
122- {
123- $ user = $ this ->verifyAndUpdateOtp ($ otp , $ otpToken );
124- return $ this ->setUpdatedTokens ($ user );
125- }
126-
127113 /**
128114 * Refresh user data
129115 * @param string $uuid
@@ -134,12 +120,27 @@ public function refreshUser(string $uuid): bool
134120 {
135121 $ user = $ this ->authService ->get ('uuid ' , $ uuid );
136122
137- if ($ user ) {
138- $ this ->setUpdatedTokens ($ user );
139- return true ;
123+ if (!$ user ) {
124+ return false ;
140125 }
141126
142- return false ;
127+ $ this ->setUpdatedTokens ($ user );
128+
129+ return true ;
130+ }
131+
132+ /**
133+ * Verify OTP
134+ * @param int $otp
135+ * @param string $otpToken
136+ * @return array
137+ * @throws AuthException
138+ * @throws JwtException
139+ */
140+ public function verifyOtp (int $ otp , string $ otpToken ): array
141+ {
142+ $ user = $ this ->verifyAndUpdateOtp ($ otp , $ otpToken );
143+ return $ this ->setUpdatedTokens ($ user );
143144 }
144145
145146 /**
Original file line number Diff line number Diff line change @@ -127,6 +127,31 @@ public function user(): ?User
127127 return null ;
128128 }
129129
130+ /**
131+ * Refresh user data
132+ * @param string $uuid
133+ * @return bool
134+ * @throws BaseException
135+ * @throws ConfigException
136+ * @throws DiException
137+ * @throws ReflectionException
138+ */
139+ public function refreshUser (string $ uuid ): bool
140+ {
141+ $ user = $ this ->authService ->get ('uuid ' , $ uuid );
142+
143+ if (!$ user ) {
144+ return false ;
145+ }
146+
147+ $ sessionData = session ()->get (self ::AUTH_USER );
148+ $ sessionData = array_merge ($ sessionData , $ this ->getVisibleFields ($ user ));
149+
150+ session ()->set (self ::AUTH_USER , $ sessionData );
151+
152+ return true ;
153+ }
154+
130155 /**
131156 * Verify OTP
132157 * @param int $otp
Original file line number Diff line number Diff line change @@ -52,12 +52,10 @@ class AccountController extends BaseController
5252 $firstname = $request -> get (' firstname' );
5353 $lastname = $request -> get (' lastname' );
5454
55- $newUserData = [
55+ $this -> authService -> update ( ' uuid ' , auth() -> user () -> uuid , [
5656 ' firstname' => $firstname ,
5757 ' lastname' => $lastname
58- ];
59-
60- $this -> authService -> update (' uuid' , auth()-> user ()-> uuid , $newUserData );
58+ ]);
6159
6260 auth()-> refreshUser (auth()-> user ()-> uuid );
6361
Original file line number Diff line number Diff line change @@ -68,20 +68,15 @@ class AccountController extends BaseController
6868 */
6969 public function update(Request $request)
7070 {
71- $firstname = $request -> get (' firstname' , null );
72- $lastname = $request -> get (' lastname' , null );
71+ $firstname = $request -> get (' firstname' );
72+ $lastname = $request -> get (' lastname' );
7373
7474 $user = $this -> authService -> update (' uuid' , auth()-> user ()-> uuid , [
7575 ' firstname' => $firstname ,
7676 ' lastname' => $lastname
7777 ]);
7878
79- $userData = session()-> get (AuthenticatableInterface::AUTH_USER);
80-
81- $userData [' firstname' ] = $user -> firstname ;
82- $userData [' lastname' ] = $user -> lastname ;
83-
84- session()-> set (AuthenticatableInterface::AUTH_USER, $userData );
79+ auth()-> refreshUser (auth()-> user ()-> uuid );
8580
8681 redirect(base_url(true ) . ' /' . current_lang() . ' /account-settings#account_profile' );
8782 }
Original file line number Diff line number Diff line change @@ -196,9 +196,11 @@ public function testApiRefreshUser()
196196 {
197197 $ this ->jwtAuth ->signin ('admin@qt.com ' , 'qwerty ' );
198198
199- $ this -> assertEquals ( ' Admin ' , $ this ->jwtAuth ->user ()-> firstname );
199+ $ user = $ this ->jwtAuth ->user ();
200200
201- $ this ->assertEquals ('User ' , $ this ->jwtAuth ->user ()->lastname );
201+ $ this ->assertEquals ('Admin ' , $ user ->firstname );
202+
203+ $ this ->assertEquals ('User ' , $ user ->lastname );
202204
203205 $ newUserData = [
204206 'firstname ' => 'Super ' ,
@@ -209,8 +211,10 @@ public function testApiRefreshUser()
209211
210212 $ this ->jwtAuth ->refreshUser ($ this ->jwtAuth ->user ()->uuid );
211213
212- $ this ->assertEquals ('Super ' , $ this ->jwtAuth ->user ()->firstname );
214+ $ refreshedUser = $ this ->jwtAuth ->user ();
215+
216+ $ this ->assertEquals ('Super ' , $ refreshedUser ->firstname );
213217
214- $ this ->assertEquals ('Human ' , $ this -> jwtAuth -> user () ->lastname );
218+ $ this ->assertEquals ('Human ' , $ refreshedUser ->lastname );
215219 }
216220}
Original file line number Diff line number Diff line change @@ -163,4 +163,30 @@ public function testWebResendOtp()
163163
164164 $ this ->assertIsString ($ this ->sessionAuth ->resendOtp ($ otp_token ));
165165 }
166+
167+ public function testWebRefreshUser ()
168+ {
169+ $ this ->sessionAuth ->signin ('admin@qt.com ' , 'qwerty ' );
170+
171+ $ user = $ this ->sessionAuth ->user ();
172+
173+ $ this ->assertEquals ('Admin ' , $ user ->firstname );
174+
175+ $ this ->assertEquals ('User ' , $ user ->lastname );
176+
177+ $ newUserData = [
178+ 'firstname ' => 'Super ' ,
179+ 'lastname ' => 'Human ' ,
180+ ];
181+
182+ $ this ->authService ->update ('uuid ' , $ user ->uuid , $ newUserData );
183+
184+ $ this ->sessionAuth ->refreshUser ($ user ->uuid );
185+
186+ $ refreshedUser = $ this ->sessionAuth ->user ();
187+
188+ $ this ->assertEquals ('Super ' , $ refreshedUser ->firstname );
189+
190+ $ this ->assertEquals ('Human ' , $ refreshedUser ->lastname );
191+ }
166192}
You can’t perform that action at this time.
0 commit comments