Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/Libraries/Auth/Adapters/JwtAuthAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Libraries/Auth/Adapters/SessionAuthAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
12 changes: 8 additions & 4 deletions tests/Unit/Libraries/Auth/Adapters/JwtAuthAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Libraries/Auth/Adapters/SessionAuthAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}