My application use JWT to handle authentication. In a testcase, I try to login with 2 users, get the tokens to call other APIs. But seem like it act as 1 users (the first one)
Check out this code, after login, I call API to get user information. But it always return the information of the user 1.
private function authenticate()
{
$user = factory(User::class)->create();
$token = \JWTAuth::fromUser($user);
return [
'Authorization' => 'Bearer ' . $token,
];
}
public function test2Login()
{
$user1 = $this->authenticate();
$user2 = $this->authenticate();
// This to make sure the token are different
$this->assertNotEquals($user1['Authorization'], $user2['Authorization']);
// Call API to get user information by the token
$response1 = $this->withHeaders($user1)->get('api/user');
$response2 = $this->withHeaders($user2)->get('api/user');
$userId1 = $response1->json()['data']['id'];
$userId2 = $response2->json()['data']['id'];
// Both response return information of the same user, so this testcase fail
$this->assertNotEquals($userId1, $userId2);
}
I expect $userId1
and $userId2
are different. For now it return the same id.
I debug into GetUserFromToken.php
, which is the Middleware of JWT. The tokens it received in both request are the same. This is weird cause in the testcase I did make sure the authenticate
function return 2 different tokens.
Anyone have any idea? Thanks
Aucun commentaire:
Enregistrer un commentaire