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:
parent
55bbe0303a
commit
09cf21243f
41
auth.js
41
auth.js
@ -2,7 +2,6 @@ const express = require("express");
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const { log } = require("./logger");
|
const { log } = require("./logger");
|
||||||
const { saveToken, getToken, deleteToken } = require("./tokenStore");
|
const { saveToken, getToken, deleteToken } = require("./tokenStore");
|
||||||
const { createFulfillmentService } = require("./fulfillmentService");
|
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@ -49,32 +48,22 @@ router.get("/auth/callback", async (req, res) => {
|
|||||||
saveToken(shop, access_token, scope);
|
saveToken(shop, access_token, scope);
|
||||||
log(shop, "Token saved to data/tokens.json");
|
log(shop, "Token saved to data/tokens.json");
|
||||||
|
|
||||||
const fulfillment = await createFulfillmentService(shop, access_token);
|
// Fetch the store's primary location ID (requires read_locations scope)
|
||||||
if (fulfillment?.success) {
|
try {
|
||||||
saveToken(shop, access_token, scope, fulfillment.fulfillmentService, fulfillment.locationId);
|
const locResp = await axios.post(
|
||||||
log(shop, "Fulfillment service and location stored");
|
`https://${shop}/admin/api/${process.env.SHOPIFY_API_VERSION || "2025-10"}/graphql.json`,
|
||||||
} else {
|
{ query: "{ locations(first: 1) { nodes { id name } } }" },
|
||||||
log(shop, `Fulfillment setup skipped/failed: ${JSON.stringify(fulfillment?.errors || fulfillment?.error || null)}`);
|
{ headers: { "X-Shopify-Access-Token": access_token, "Content-Type": "application/json" } }
|
||||||
}
|
);
|
||||||
|
const node = locResp.data?.data?.locations?.nodes?.[0];
|
||||||
// Fallback: always try to resolve a locationId from the store's primary location
|
if (node?.id) {
|
||||||
const saved = getToken(shop);
|
saveToken(shop, access_token, scope, null, node.id);
|
||||||
if (!saved?.locationId) {
|
log(shop, `Primary location saved: ${node.name} (${node.id})`);
|
||||||
try {
|
} else {
|
||||||
const locResp = await axios.post(
|
log(shop, `No location returned — add read_locations to SHOPIFY_SCOPES and re-auth.`);
|
||||||
`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}`);
|
|
||||||
}
|
}
|
||||||
|
} catch (locErr) {
|
||||||
|
log(shop, `Could not fetch primary location: ${locErr.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const finalToken = getToken(shop);
|
const finalToken = getToken(shop);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user