Hi there!
I am building a Shopify public app (sales channel) using Shopify Polaris and Laravel, hosted on Heroku. I created the app on the Shopify Partner account dashboard and configured it with an app URL and a redirection URL. For the app URL, I added the public link for the Laravel application provided by Heroku. And for the redirection URL, I added the same link followed by /auth/shopify.
After that I set laravel routes as follows.
Route::get('/auth/shopify', [ShopifyAuthController::class, 'redirectToShopify']);
Route::get('/auth/shopify/callback', [ShopifyAuthController::class, 'handleShopifyCallback']);
And here are functions in ShopifyAuthController.
public function redirectToShopify()
{
$shopifyDomain = request('shop');
$scopes = ['read_products', 'write_products']; // Add necessary scopes for your app
$query = http_build_query([
'client_id' => env('SHOPIFY_API_KEY'),
'redirect_uri' => env('SHOPIFY_REDIRECT_URI'),
'scope' => implode(',', $scopes),
'state' => csrf_token(),
]);
// Construct the OAuth authorization URL
$authorizationUrl = "https://{$shopifyDomain}/admin/oauth/authorize?{$query}";
return redirect($authorizationUrl);
}
public function handleShopifyCallback()
{
$code = request('code');
$state = request('state');
if ($state === null || !hash_equals(csrf_token(), $state)) {
abort(403, 'Invalid state parameter.');
}
// Extract store domain from the callback URL
$storeDomain = request('shop'); // 'shop' is the query parameter containing the store domain
// Construct the OAuth endpoint URL for the specific store
$oauthEndpointUrl = "https://{$storeDomain}/admin/oauth/access_token";
try {
$response = Http::post($oauthEndpointUrl, [
'client_id' => env('SHOPIFY_API_KEY'),
'client_secret' => env('SHOPIFY_API_SECRET'),
'code' => $code,
]);
$responseBody = $response->json();
if (isset($responseBody['access_token'])) {
return redirect('/home');
} else {
throw new \Exception('Access token not found in response.');
}
} catch (\Exception $e) {
// Log or handle the error appropriately
return response()->json(['error' => $e->getMessage()], 500);
}
}
Now when I click on the ‘Install app’ button to add the app to my dev store, it redirects to the application hosted on Heroku instead of being embedded in the Shopify dashboard. Additionally, in the inspect console, I can see the following information.
hmac: xxxxx
host: xxxxx
shop: sales-channel-test.myshopify.com
timestamp: 1713581168
I’m not sure what is missing from my configuration. I think the authentication redirection callback isn’t working.
Could you help me resolve this?
Thank you!