diff --git a/src/Libraries/Auth/Adapters/JwtAuthAdapter.php b/src/Libraries/Auth/Adapters/JwtAuthAdapter.php index 8d5fa7b3..30f331ac 100644 --- a/src/Libraries/Auth/Adapters/JwtAuthAdapter.php +++ b/src/Libraries/Auth/Adapters/JwtAuthAdapter.php @@ -110,20 +110,6 @@ public function user(): ?User } } - /** - * Verify OTP - * @param int $otp - * @param string $otpToken - * @return array - * @throws AuthException - * @throws JwtException - */ - public function verifyOtp(int $otp, string $otpToken): array - { - $user = $this->verifyAndUpdateOtp($otp, $otpToken); - return $this->setUpdatedTokens($user); - } - /** * Refresh user data * @param string $uuid @@ -134,12 +120,27 @@ public function refreshUser(string $uuid): bool { $user = $this->authService->get('uuid', $uuid); - if($user) { - $this->setUpdatedTokens($user); - return true; + if(!$user) { + return false; } - return false; + $this->setUpdatedTokens($user); + + return true; + } + + /** + * Verify OTP + * @param int $otp + * @param string $otpToken + * @return array + * @throws AuthException + * @throws JwtException + */ + public function verifyOtp(int $otp, string $otpToken): array + { + $user = $this->verifyAndUpdateOtp($otp, $otpToken); + return $this->setUpdatedTokens($user); } /** diff --git a/src/Libraries/Auth/Adapters/SessionAuthAdapter.php b/src/Libraries/Auth/Adapters/SessionAuthAdapter.php index 70b48550..5fd9cfef 100644 --- a/src/Libraries/Auth/Adapters/SessionAuthAdapter.php +++ b/src/Libraries/Auth/Adapters/SessionAuthAdapter.php @@ -127,6 +127,31 @@ public function user(): ?User return null; } + /** + * Refresh user data + * @param string $uuid + * @return bool + * @throws BaseException + * @throws ConfigException + * @throws DiException + * @throws ReflectionException + */ + public function refreshUser(string $uuid): bool + { + $user = $this->authService->get('uuid', $uuid); + + if (!$user) { + return false; + } + + $sessionData = session()->get(self::AUTH_USER); + $sessionData = array_merge($sessionData, $this->getVisibleFields($user)); + + session()->set(self::AUTH_USER, $sessionData); + + return true; + } + /** * Verify OTP * @param int $otp diff --git a/src/Module/Templates/DemoApi/src/Controllers/AccountController.php.tpl b/src/Module/Templates/DemoApi/src/Controllers/AccountController.php.tpl index 5e518b24..c93f64e4 100644 --- a/src/Module/Templates/DemoApi/src/Controllers/AccountController.php.tpl +++ b/src/Module/Templates/DemoApi/src/Controllers/AccountController.php.tpl @@ -52,12 +52,10 @@ class AccountController extends BaseController $firstname = $request->get('firstname'); $lastname = $request->get('lastname'); - $newUserData = [ + $this->authService->update('uuid', auth()->user()->uuid, [ 'firstname' => $firstname, 'lastname' => $lastname - ]; - - $this->authService->update('uuid', auth()->user()->uuid, $newUserData); + ]); auth()->refreshUser(auth()->user()->uuid); diff --git a/src/Module/Templates/DemoWeb/src/Controllers/AccountController.php.tpl b/src/Module/Templates/DemoWeb/src/Controllers/AccountController.php.tpl index 76ef740c..09c97ccb 100644 --- a/src/Module/Templates/DemoWeb/src/Controllers/AccountController.php.tpl +++ b/src/Module/Templates/DemoWeb/src/Controllers/AccountController.php.tpl @@ -68,20 +68,15 @@ class AccountController extends BaseController */ public function update(Request $request) { - $firstname = $request->get('firstname', null); - $lastname = $request->get('lastname', null); + $firstname = $request->get('firstname'); + $lastname = $request->get('lastname'); $user = $this->authService->update('uuid', auth()->user()->uuid, [ 'firstname' => $firstname, 'lastname' => $lastname ]); - $userData = session()->get(AuthenticatableInterface::AUTH_USER); - - $userData['firstname'] = $user->firstname; - $userData['lastname'] = $user->lastname; - - session()->set(AuthenticatableInterface::AUTH_USER, $userData); + auth()->refreshUser(auth()->user()->uuid); redirect(base_url(true) . '/' . current_lang() . '/account-settings#account_profile'); } diff --git a/tests/Unit/Libraries/Auth/Adapters/JwtAuthAdapterTest.php b/tests/Unit/Libraries/Auth/Adapters/JwtAuthAdapterTest.php index 487035b4..98aedf7e 100644 --- a/tests/Unit/Libraries/Auth/Adapters/JwtAuthAdapterTest.php +++ b/tests/Unit/Libraries/Auth/Adapters/JwtAuthAdapterTest.php @@ -196,9 +196,11 @@ public function testApiRefreshUser() { $this->jwtAuth->signin('admin@qt.com', 'qwerty'); - $this->assertEquals('Admin', $this->jwtAuth->user()->firstname); + $user = $this->jwtAuth->user(); - $this->assertEquals('User', $this->jwtAuth->user()->lastname); + $this->assertEquals('Admin', $user->firstname); + + $this->assertEquals('User', $user->lastname); $newUserData = [ 'firstname' => 'Super', @@ -209,8 +211,10 @@ public function testApiRefreshUser() $this->jwtAuth->refreshUser($this->jwtAuth->user()->uuid); - $this->assertEquals('Super', $this->jwtAuth->user()->firstname); + $refreshedUser = $this->jwtAuth->user(); + + $this->assertEquals('Super', $refreshedUser->firstname); - $this->assertEquals('Human', $this->jwtAuth->user()->lastname); + $this->assertEquals('Human', $refreshedUser->lastname); } } \ No newline at end of file diff --git a/tests/Unit/Libraries/Auth/Adapters/SessionAuthAdapterTest.php b/tests/Unit/Libraries/Auth/Adapters/SessionAuthAdapterTest.php index 62885dd6..06b19eaa 100644 --- a/tests/Unit/Libraries/Auth/Adapters/SessionAuthAdapterTest.php +++ b/tests/Unit/Libraries/Auth/Adapters/SessionAuthAdapterTest.php @@ -163,4 +163,30 @@ public function testWebResendOtp() $this->assertIsString($this->sessionAuth->resendOtp($otp_token)); } + + public function testWebRefreshUser() + { + $this->sessionAuth->signin('admin@qt.com', 'qwerty'); + + $user = $this->sessionAuth->user(); + + $this->assertEquals('Admin', $user->firstname); + + $this->assertEquals('User', $user->lastname); + + $newUserData = [ + 'firstname' => 'Super', + 'lastname' => 'Human', + ]; + + $this->authService->update('uuid', $user->uuid, $newUserData); + + $this->sessionAuth->refreshUser($user->uuid); + + $refreshedUser = $this->sessionAuth->user(); + + $this->assertEquals('Super', $refreshedUser->firstname); + + $this->assertEquals('Human', $refreshedUser->lastname); + } } \ No newline at end of file