public function login(string $ticketUuidOrRefreshToken, string $code = null) 
    {
        if ($code === null) {
            $response = $this->httpClient()->post(self::AUTH_API . 'oauth/token', [
                "app_context" => "inapp_ios",
                "client_id" => self::CLIENT_ID,
                "client_secret" => self::CLIENT_SECRET,
                "refresh_token" => $ticketUuidOrRefreshToken,
                "duid" => self::DUID,
                "grant_type" => "refresh_token",
                "scope" => self::SCOPE
            ]);
        } else {
            $response = $this->httpClient()->post(self::AUTH_API . 'ssocookie', [
                'authentication_type' => 'two_step',
                'ticket_uuid' => $ticketUuidOrRefreshToken,
                'code' => $code,
                'client_id' => self::CLIENT_ID
            ]);

            $npsso = $response->npsso;

            $response = $this->httpClient()->get(self::AUTH_API . 'oauth/authorize', [
                'duid' => self::DUID,
                'client_id' => self::CLIENT_ID,
                'response_type' => 'code',
                'scope' => self::SCOPE,
                'redirect_uri' => 'com.playstation.PlayStationApp://redirect'
            ], [
                'Cookie' => 'npsso=' . $npsso
            ]);

            if (($response instanceof Response) === false) {
                throw new \Exception('Unexpected response');
            }

            $grant = $response->getHeaderLine('X-NP-GRANT-CODE');

            if (empty($grant)) {
                throw new \Exception('Unable to get X-NP-GRANT-CODE');
            }

            $response = $this->httpClient()->post(self::AUTH_API . 'oauth/token', [
                'client_id' => self::CLIENT_ID,
                'client_secret' => self::CLIENT_SECRET,
                'duid' => self::DUID,
                'scope' => self::SCOPE,
                'redirect_uri' => 'com.playstation.PlayStationApp://redirect',
                'code' => $grant,
                'grant_type' => 'authorization_code'
            ]);

        }

        $this->accessToken = $response->access_token;
        $this->refreshToken = $response->refresh_token;
        $this->expiresIn = $response->expires_in;

        $handler = \GuzzleHttp\HandlerStack::create();
        $handler->push(Middleware::mapRequest(new TokenMiddleware($this->accessToken(), $this->refreshToken(), $this->expireDate())));

        $newOptions = array_merge(['handler' => $handler], $this->options);
    
        $this->httpClient = new HttpClient(new \GuzzleHttp\Client($newOptions));
    }