Skip to content

Commit a3a272e

Browse files
authored
Merge pull request #330 from armanist/329-Add-refreshUser()-method-to-SessionAuthAdapter
Add `refreshUser()` method to `SessionAuthAdapter`
2 parents 67e20fb + 17567cb commit a3a272e

File tree

6 files changed

+83
-34
lines changed

6 files changed

+83
-34
lines changed

src/Libraries/Auth/Adapters/JwtAuthAdapter.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff 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
/**

src/Libraries/Auth/Adapters/SessionAuthAdapter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff 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

src/Module/Templates/DemoApi/src/Controllers/AccountController.php.tpl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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

src/Module/Templates/DemoWeb/src/Controllers/AccountController.php.tpl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff 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
}

tests/Unit/Libraries/Auth/Adapters/JwtAuthAdapterTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff 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
}

tests/Unit/Libraries/Auth/Adapters/SessionAuthAdapterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)