Shopify/shopify-api-php

Could not find the current session id in the cookies - Error still happening

robertiulianstoica opened this issue · 2 comments

Issue summary

When installing the app, i am redirected to the oauth, after i approve the scopes for the app i am redirected to the callback i get the "Could not find the current session id in the cookies" error.

Expected behavior

After calback i should receive a permanent token and save it in the database.

Actual behavior

I am redirected to shopify and i see this "Could not find the current session id in the cookies" error.

Steps to reproduce the problem

I am using symfony 5.4 and have not managed to get my head around this.

namespace App\Merchant\Service;

use App\Merchant\Dto\OrderResponse;
use Doctrine\Persistence\ManagerRegistry;
use Shopify\Auth\FileSessionStorage;
use Shopify\Auth\OAuth;
use Shopify\Auth\Session;
use Shopify\Clients\Rest;
use Shopify\Context;
use Shopify\Utils;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class MerchantService
{
    private $session;

    public function __construct(
        ContainerBagInterface $params,
        HttpClientInterface $http_client,
        ManagerRegistry $doctrine
    ) {
        Context::initialize(
            apiKey: $params->get('merchant_client_id'),
            apiSecretKey: $params->get('merchant_client_secret'),
            scopes: $params->get('merchant_scopes'),
            hostName: $params->get('merchant_hostname'),
            sessionStorage: new FileSessionStorage(),
            apiVersion: '2023-01',
            isEmbeddedApp: true,
            isPrivateApp: false,
        );
    }

    public function authenticate(string $shop): string
    {
        return OAuth::begin(
            $shop,
            'shopify/callback',
            true
        );
    }

    public function callback(array $cookies, array $query)
    {
        return OAuth::callback(
            $cookies,
            $query
        );
    }
}

I have tried switching to an older API version but it still deos not work.

namespace App\Merchant\Controller;

use App\Customer\Service\CustomerService;
use App\Merchant\Service\MerchantService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Stopwatch\Stopwatch;

class AuthenticationController extends AbstractController
{
    #[Route('/oauth', name: 'oauth', methods: ['GET'])]
    public function oauth(
        Request $request,
        MerchantService $merchant,
        ContainerBagInterface $params,
    ): Response {
        return $this->redirect($merchant->authenticate($request->get('shop')));
    }

    #[Route('/callback', name: 'callback', methods: ['GET'])]
    public function callback(
        Request $request,
        Stopwatch $stopwatch,
        MerchantService $merchant,
        CustomerService $customer,
    ): Response {
        $cookies = $request->cookies->all();
        if (null == $cookies) {
            $cookies = $_COOKIE;
        }

        $session = $merchant->callback($cookies, $request->query->all());

        $customer->saveAccessToken($session->getShop(), $session->getAccessToken());

        return $this->redirectToRoute('install', $request->query->all());
    }
}


Checklist

  • I have described this issue in a way that is actionable (if possible)

Am also facing the same issue, Is this because of laravel update ?
Am using laravel 10.0

I managed to find a fix for it. The problem appears to be on the callback when using Oauth::callback.

Replacing this

public function callback(array $cookies, array $query)
{
    return OAuth::callback(
        $cookies,
        $query
    );
}

with this

public function callback(Request $request): Session
{
    $response = $this->client->request('POST', 'https://'.$request->get('shop').'/admin/oauth/access_token', [
        'json' => [
            'client_id' => $this->apiKey,
            'client_secret' => $this->apiSecretKey,
            'code' => $request->get('code'),
        ],
    ]);

    $data = $response->toArray();
    $accessToken = $data['access_token'];

    return $this->createSession($request->get('shop'), $accessToken);
}

seems to work correctly.
Practically i needed to get the access token yourself and then create a session using the Shopify API.
This also allows me to use app bridge while avoiding the redirect issue for it.