jackwener/OpenCLI · error · AuthRequiredError

jd add-cart requires a logged-in JD session

Error message

jd add-cart requires a logged-in JD session

What it means

jd add-cart throws AuthRequiredError when the headless probe of the JD cart page reports 'login_required' — i.e. the browser session attached to the command has no valid logged-in JD identity. The command refuses to act anonymously because adding to a cart requires a bound account. AuthRequiredError carries code AUTH_REQUIRED and exit code NOPERM with a hint to log in to https://jd.com in Chrome.

Source

Thrown at clis/jd/add-cart.js:58

                }];
        }
        await page.goto(`https://cart.jd.com/gate.action?pid=${sku}&pcount=${num}&ptype=1`);
        await page.wait(4);
        const result = await page.evaluate(`
      (() => {
        const url = location.href;
        const text = document.body?.innerText || '';
        if (text.includes('已成功加入') || text.includes('商品已成功') || url.includes('addtocart')) {
          return 'success';
        }
        if (text.includes('请登录') || text.includes('login') || url.includes('login')) {
          return 'login_required';
        }
        return 'page:' + url.substring(0, 60) + ' | ' + text.substring(0, 100);
      })()
    `);
        if (result === 'login_required') {
            throw new AuthRequiredError('jd add-cart requires a logged-in JD session');
        }
        let status = '? 未知';
        if (result === 'success')
            status = '✓ 已加入购物车';
        else
            status = '? ' + result;
        return [{
                status,
                title: (info?.title || '').slice(0, 80),
                price: info?.price || '',
                sku,
            }];
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Open Chrome/Chromium in the profile the CLI uses and log in to https://www.jd.com
  2. Re-run the auth check first (e.g. `jd auth login` / verify) before add-cart
  3. Clear stale JD cookies and log in fresh if the session is half-expired
  4. Retry after login; if it persists, check whether JD is showing a captcha/anti-bot page

Example fix

// before
await jdAddCart(sku);
// after
try {
  await jdAddCart(sku);
} catch (e) {
  if (e.code === 'AUTH_REQUIRED') {
    await jdAuthLogin(); // log in via browser
    await jdAddCart(sku);
  }
}
Defensive patterns

Strategy: try-catch

Validate before calling

const cookies = await page.getCookies({ url: 'https://www.jd.com' });
const loggedIn = cookies.some(c => (c.name === 'pin' || c.name === 'thor') && c.value);
if (!loggedIn) throw new Error('Run jd auth login before add-cart');

Type guard

function isAuthRequired(e) { return e && e.code === 'AUTH_REQUIRED'; }

Try / catch

try {
  await jdAddCart(sku);
} catch (e) {
  if (isAuthRequired(e)) {
    await jdAuthLogin();
    await jdAddCart(sku);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `jd add-cart <sku>` while the connected Chrome profile has no JD pin/thor session cookies, after the JD session expired server-side, or when JD redirects the cart page to a login wall which the in-page evaluate detects and returns 'login_required'.

Common situations: Session cookies expired (JD sessions are short-lived and rotate); user logged out in the browser the CLI controls; automation profile never logged in to JD; JD served an anti-bot page misread as logged-out.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/21b3027992cf2a84. Report an issue: GitHub.