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 <noreply@anthropic.com>
This commit is contained in:
MOHAN 2026-08-22 00:37:47 +05:30
parent 55bbe0303a
commit 09cf21243f

25
auth.js
View File

@ -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,33 +48,23 @@ 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) {
// 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 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})`);
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);
log(shop, `Auth complete. Stored token:\n${JSON.stringify(finalToken, null, 2)}`);