From 09cf21243fe118afab337499941e0273a95ec84a Mon Sep 17 00:00:00 2001 From: MOHAN Date: Sat, 22 Aug 2026 00:37:47 +0530 Subject: [PATCH] fix(auth): simplify to just fetch primary store location after OAuth Removes fulfillment service creation entirely. Fetches the store's primary location ID via locations(first:1) query. Requires read_locations scope in SHOPIFY_SCOPES env var. Co-Authored-By: Claude Sonnet 4.6 --- auth.js | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/auth.js b/auth.js index 7bf1cd1..4b55b33 100644 --- a/auth.js +++ b/auth.js @@ -2,7 +2,6 @@ const express = require("express"); const axios = require("axios"); const { log } = require("./logger"); const { saveToken, getToken, deleteToken } = require("./tokenStore"); -const { createFulfillmentService } = require("./fulfillmentService"); const router = express.Router(); @@ -49,32 +48,22 @@ router.get("/auth/callback", async (req, res) => { saveToken(shop, access_token, scope); log(shop, "Token saved to data/tokens.json"); - const fulfillment = await createFulfillmentService(shop, access_token); - if (fulfillment?.success) { - saveToken(shop, access_token, scope, fulfillment.fulfillmentService, fulfillment.locationId); - log(shop, "Fulfillment service and location stored"); - } else { - log(shop, `Fulfillment setup skipped/failed: ${JSON.stringify(fulfillment?.errors || fulfillment?.error || null)}`); - } - - // Fallback: always try to resolve a locationId from the store's primary location - const saved = getToken(shop); - if (!saved?.locationId) { - try { - const locResp = await axios.post( - `https://${shop}/admin/api/${process.env.SHOPIFY_API_VERSION || "2025-10"}/graphql.json`, - { query: "{ locations(first: 1) { nodes { id name } } }" }, - { headers: { "X-Shopify-Access-Token": access_token, "Content-Type": "application/json" } } - ); - const primaryLocId = locResp.data?.data?.locations?.nodes?.[0]?.id || null; - const primaryLocName = locResp.data?.data?.locations?.nodes?.[0]?.name || null; - if (primaryLocId) { - saveToken(shop, access_token, scope, saved?.fulfillmentService || null, primaryLocId); - log(shop, `Primary location saved: ${primaryLocName} (${primaryLocId})`); - } - } catch (locErr) { - log(shop, `Could not fetch primary location: ${locErr.message}`); + // Fetch the store's primary location ID (requires read_locations scope) + try { + const locResp = await axios.post( + `https://${shop}/admin/api/${process.env.SHOPIFY_API_VERSION || "2025-10"}/graphql.json`, + { query: "{ locations(first: 1) { nodes { id name } } }" }, + { headers: { "X-Shopify-Access-Token": access_token, "Content-Type": "application/json" } } + ); + const node = locResp.data?.data?.locations?.nodes?.[0]; + if (node?.id) { + saveToken(shop, access_token, scope, null, node.id); + log(shop, `Primary location saved: ${node.name} (${node.id})`); + } else { + log(shop, `No location returned — add read_locations to SHOPIFY_SCOPES and re-auth.`); } + } catch (locErr) { + log(shop, `Could not fetch primary location: ${locErr.message}`); } const finalToken = getToken(shop);