From b4ab8cf6d2e31b7119f8ee9a73eb4f61143a3c11 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Tue, 23 Jan 2018 20:50:14 -0800 Subject: [PATCH] Making login/logout work as expected with newer controllers (persisting cookies within a client, except after login/logout) --- src/Client.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 135fc9f..8e7a824 100644 --- a/src/Client.php +++ b/src/Client.php @@ -48,6 +48,11 @@ class Client */ private $client; + /** + * @var CookieJar + */ + private $cookieJar; + /** * @var array */ @@ -62,6 +67,7 @@ class Client public function __construct(ClientInterface $client, array $requestOptions = []) { $this->client = $client; + $this->cookieJar = new CookieJar(); $this->requestOptions = $this->getRequestOptions($requestOptions); } @@ -76,9 +82,15 @@ class Client */ public function login($username, $password) { + $this->cookieJar = new CookieJar(); // reset cookies during login $this->post( '/api/login', - ['username' => $username, 'password' => $password] + [ + 'username' => $username, + 'password' => $password, + 'remember' => true, + 'strict' => false + ] ); } @@ -87,6 +99,7 @@ class Client */ public function logout() { + $this->cookieJar = new CookieJar(); // reset cookies during logout $this->client->request('get', '/logout', ['allow_redirects' => false] + $this->requestOptions); } @@ -200,11 +213,16 @@ class Client return $this->client->request('get', $url, $requestOptions); } + public function getCookieJar() + { + return $this->cookieJar; + } + private function getRequestOptions(array $defaultRequestOptions) { return array_merge( [ - 'cookies' => new CookieJar(), + 'cookies' => $this->cookieJar, 'verify' => false ], $defaultRequestOptions