Skip to content

Commit f552ad6

Browse files
committed
Extract JWT generation to re-useable public function
1 parent 9104c8a commit f552ad6

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
### 3.0.5
4+
5+
* Changed: Extract JWT token generation to a public function so it can be used from other places.
6+
37
### 3.0.4
48

59
* Added: Allowed OPTIONS header

src/TokenBasedRestApiModule.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ protected function authenticate(Request $request)
119119
}
120120
}
121121

122+
public function createJwtTokenForLoggedInUser($authData) : string {
123+
$expiry = new \DateTime('now +1 day');
124+
125+
return JWT::encode(
126+
[
127+
'expires' => $expiry->getTimestamp(),
128+
'user' => $authData,
129+
],
130+
$this->secret,
131+
$this->algorithm
132+
);
133+
}
134+
122135
public function registerErrorHandlers(App $app)
123136
{
124137

@@ -159,18 +172,8 @@ public function registerRoutes(App $app)
159172
list($status, $authData) = $self->authenticate($request);
160173

161174
if ($status) {
162-
$expiry = new \DateTime();
163-
$expiry->add(new\DateInterval('P1D'));
164-
165175
$data = [
166-
'token' => JWT::encode(
167-
[
168-
'expires' => $expiry->getTimestamp(),
169-
'user' => $authData,
170-
],
171-
$self->secret,
172-
$self->algorithm
173-
)
176+
'token' => $self->createJwtTokenForLoggedInUser($authData)
174177
];
175178

176179
return $response
@@ -193,7 +196,7 @@ public function registerRoutes(App $app)
193196
/** @var LoginProvider $login */
194197
$login = LoginProvider::getProvider();
195198
$adapter = new UserEntityAdapter();
196-
return $adapter->put($request, $response, $login->loggedInUserIdentifier);
199+
return $adapter->put($request, $response, $login->loggedInUserIdentifier, $login->loggedInUserIdentifier);
197200
});
198201
}
199202
}

0 commit comments

Comments
 (0)