Making login/logout work as expected with newer controllers (persisting cookies within a client, except after login/logout)

This commit is contained in:
Will Bradley 2018-01-23 20:50:14 -08:00
parent d54f15be2a
commit b4ab8cf6d2

View File

@ -48,6 +48,11 @@ class Client
*/ */
private $client; private $client;
/**
* @var CookieJar
*/
private $cookieJar;
/** /**
* @var array * @var array
*/ */
@ -62,6 +67,7 @@ class Client
public function __construct(ClientInterface $client, array $requestOptions = []) public function __construct(ClientInterface $client, array $requestOptions = [])
{ {
$this->client = $client; $this->client = $client;
$this->cookieJar = new CookieJar();
$this->requestOptions = $this->getRequestOptions($requestOptions); $this->requestOptions = $this->getRequestOptions($requestOptions);
} }
@ -76,9 +82,15 @@ class Client
*/ */
public function login($username, $password) public function login($username, $password)
{ {
$this->cookieJar = new CookieJar(); // reset cookies during login
$this->post( $this->post(
'/api/login', '/api/login',
['username' => $username, 'password' => $password] [
'username' => $username,
'password' => $password,
'remember' => true,
'strict' => false
]
); );
} }
@ -87,6 +99,7 @@ class Client
*/ */
public function logout() public function logout()
{ {
$this->cookieJar = new CookieJar(); // reset cookies during logout
$this->client->request('get', '/logout', ['allow_redirects' => false] + $this->requestOptions); $this->client->request('get', '/logout', ['allow_redirects' => false] + $this->requestOptions);
} }
@ -200,11 +213,16 @@ class Client
return $this->client->request('get', $url, $requestOptions); return $this->client->request('get', $url, $requestOptions);
} }
public function getCookieJar()
{
return $this->cookieJar;
}
private function getRequestOptions(array $defaultRequestOptions) private function getRequestOptions(array $defaultRequestOptions)
{ {
return array_merge( return array_merge(
[ [
'cookies' => new CookieJar(), 'cookies' => $this->cookieJar,
'verify' => false 'verify' => false
], ],
$defaultRequestOptions $defaultRequestOptions