jackwener/OpenCLI · error · AuthRequiredError

jd.com

Error message

jd.com

What it means

verifyJdIdentity throws AuthRequiredError('jd.com') when the browser's cookie jar for JD contains neither the 'pin' nor 'thor' cookie, meaning no logged-in JD session exists. This is the library's pre-flight identity check before probing account details on home.jd.com.

Source

Thrown at clis/jd/auth.js:12

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';

async function hasJdSessionCookie(page) {
  const cookies = await page.getCookies({ url: 'https://www.jd.com' });
  const names = new Set(cookies.map(c => c.name));
  return names.has('pin') || names.has('thor');
}

async function verifyJdIdentity(page) {
  if (!await hasJdSessionCookie(page)) {
    throw new AuthRequiredError('jd.com', 'JD pin / thor cookie missing');
  }
  await page.goto('https://home.jd.com/');
  await page.wait(3);
  const probe = await page.evaluate(`
    (() => {
      const pinCookie = (document.cookie.split('; ').find(c => c.startsWith('pin=')) || '').split('=')[1] || '';
      const decoded = pinCookie ? decodeURIComponent(pinCookie) : '';
      if (!decoded) {
        return { kind: 'auth', detail: 'JD pin cookie empty after decode' };
      }
      const nickEl = document.querySelector('.user-info, #aliveUserName, .name, .user-name');
      const nickname = (nickEl && nickEl.textContent && nickEl.textContent.trim()) || '';
      return { ok: true, pin: decoded, nickname };
    })()
  `);
  if (probe?.kind === 'auth') throw new AuthRequiredError('jd.com', probe.detail);
  if (!probe?.ok) throw new CommandExecutionError(`Unexpected JD probe: ${JSON.stringify(probe)}`);
  return { pin: probe.pin, nickname: probe.nickname };

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log in to https://www.jd.com in the Chrome profile the CLI is connected to
  2. Run `jd auth login` and complete the browser login flow
  3. Verify the CLI is attached to the intended browser profile (one with JD cookies)
  4. After login, re-run the verify/whoami command

Example fix

// before
const me = await jdWhoami();
// after
if (!(await jdHasSessionCookie())) {
  await jdAuthLogin();
}
const me = await jdWhoami();
Defensive patterns

Strategy: validation

Validate before calling

const cookies = await page.getCookies({ url: 'https://www.jd.com' });
const names = new Set(cookies.map(c => c.name));
if (!names.has('pin') && !names.has('thor')) {
  await jdAuthLogin(); // pre-flight login before verify
}

Type guard

function hasJdSession(cookies) {
  const names = new Set(cookies.map(c => c.name));
  return names.has('pin') || names.has('thor');
}

Try / catch

try {
  const me = await jdVerify();
} catch (e) {
  if (e.code === 'AUTH_REQUIRED') return loginAndRetry();
  throw e;
}

Prevention

When it happens

Trigger: Any jd auth verify / whoami / login poll invocation where page.getCookies() for JD returns no 'pin' and no 'thor' cookie — anonymous profile, logged-out browser, or cookies cleared.

Common situations: Fresh automation profile never logged in to JD; user logged out manually; JD expired/cleared the session cookie; wrong Chrome profile connected (cookies live in another profile).

Related errors


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