{"record":{"id":"21b3027992cf2a84","repo":"jackwener/OpenCLI","slug":"jd-add-cart-requires-a-logged-in-jd-session","errorCode":null,"errorMessage":"jd add-cart requires a logged-in JD session","messagePattern":"jd add-cart requires a logged-in JD session","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/jd/add-cart.js","lineNumber":58,"sourceCode":"                }];\n        }\n        await page.goto(`https://cart.jd.com/gate.action?pid=${sku}&pcount=${num}&ptype=1`);\n        await page.wait(4);\n        const result = await page.evaluate(`\n      (() => {\n        const url = location.href;\n        const text = document.body?.innerText || '';\n        if (text.includes('已成功加入') || text.includes('商品已成功') || url.includes('addtocart')) {\n          return 'success';\n        }\n        if (text.includes('请登录') || text.includes('login') || url.includes('login')) {\n          return 'login_required';\n        }\n        return 'page:' + url.substring(0, 60) + ' | ' + text.substring(0, 100);\n      })()\n    `);\n        if (result === 'login_required') {\n            throw new AuthRequiredError('jd add-cart requires a logged-in JD session');\n        }\n        let status = '? 未知';\n        if (result === 'success')\n            status = '✓ 已加入购物车';\n        else\n            status = '? ' + result;\n        return [{\n                status,\n                title: (info?.title || '').slice(0, 80),\n                price: info?.price || '',\n                sku,\n            }];\n    },\n});\n","sourceCodeStart":40,"sourceCodeEnd":73,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/jd/add-cart.js#L40-L73","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","solutions":["Open Chrome/Chromium in the profile the CLI uses and log in to https://www.jd.com","Re-run the auth check first (e.g. `jd auth login` / verify) before add-cart","Clear stale JD cookies and log in fresh if the session is half-expired","Retry after login; if it persists, check whether JD is showing a captcha/anti-bot page"],"exampleFix":"// before\nawait jdAddCart(sku);\n// after\ntry {\n  await jdAddCart(sku);\n} catch (e) {\n  if (e.code === 'AUTH_REQUIRED') {\n    await jdAuthLogin(); // log in via browser\n    await jdAddCart(sku);\n  }\n}","handlingStrategy":"try-catch","validationCode":"const cookies = await page.getCookies({ url: 'https://www.jd.com' });\nconst loggedIn = cookies.some(c => (c.name === 'pin' || c.name === 'thor') && c.value);\nif (!loggedIn) throw new Error('Run jd auth login before add-cart');","typeGuard":"function isAuthRequired(e) { return e && e.code === 'AUTH_REQUIRED'; }","tryCatchPattern":"try {\n  await jdAddCart(sku);\n} catch (e) {\n  if (isAuthRequired(e)) {\n    await jdAuthLogin();\n    await jdAddCart(sku);\n  } else throw e;\n}","preventionTips":["Run jd auth verify before any cart-write command","Keep the browser profile logged in to JD and check session freshness periodically","Handle AUTH_REQUIRED centrally in your CLI wrapper with a re-login step","Avoid long-lived scripts that assume the JD session never expires"],"tags":["auth","jd","login-required","browser-session"],"backgroundTag":"site-session-not-logged-in","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}