jackwener/OpenCLI · error · AuthRequiredError

${probe.detail}

Error message

${probe.detail}

What it means

AuthRequiredError rethrown from verifyToutiaoIdentity when the in-page probe returns {kind:'auth'}. Either mp.toutiao.com redirected to /auth/page/login (anonymous) or the dashboard rendered but no user_id could be extracted from __INITIAL_STATE__/__REDUX_STATE__/__SSR_DATA__, the sso_uid cookie, or DOM selectors — interpreted as an anonymous or stale session.

Source

Thrown at clis/toutiao/auth.js:52

          }
          for (const v of Object.values(node)) if (v && typeof v === 'object') stack.push(v);
        }
      } catch {}
      if (!userId) {
        const ssoUid = (document.cookie.split('; ').find(c => c.startsWith('sso_uid=')) || '').split('=')[1] || '';
        if (ssoUid) userId = ssoUid;
      }
      if (!nickname) {
        const el = document.querySelector('.user-name, .header-username, .avatar-name, [class*="userName"]');
        nickname = (el?.innerText || '').trim();
      }
      if (!userId) {
        return { kind: 'auth', detail: 'Toutiao dashboard rendered but no user_id surface — stale session' };
      }
      return { ok: true, user_id: userId, nickname };
    })()
  `);
  if (probe?.kind === 'auth') throw new AuthRequiredError('toutiao.com', probe.detail);
  if (!probe?.ok) throw new CommandExecutionError(`Unexpected Toutiao probe: ${JSON.stringify(probe)}`);
  return { user_id: probe.user_id, nickname: probe.nickname };
}

registerSiteAuthCommands({
  site: 'toutiao',
  domain: 'toutiao.com',
  loginUrl: 'https://mp.toutiao.com/auth/page/login',
  columns: ['user_id', 'nickname'],
  quickCheck: hasToutiaoSessionCookie,
  verify: verifyToutiaoIdentity,
  poll: async (page) => {
    if (!await hasToutiaoSessionCookie(page)) {
      throw new AuthRequiredError('toutiao.com', 'Waiting for Toutiao sessionid cookie');
    }
    return verifyToutiaoIdentity(page);
  },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log in again at https://mp.toutiao.com/auth/page/login — a present-but-stale sessionid cookie is the most common cause — then re-run verify.
  2. Confirm manually in the same browser profile that mp.toutiao.com shows the dashboard without redirecting to login.
  3. Clear mp.toutiao.com cookies and do a fresh login so no stale sessionid remains.
  4. If the dashboard renders fine for a human but the probe still finds no user_id, update the probe's state keys/selectors in clis/toutiao/auth.js to the current site layout.
  5. Wait a moment after login completes before verifying; a redirect race right after login can trigger this transiently.

Example fix

// before: stale cookie makes the probe return {kind:'auth'}
await toutiaoAuthVerify(page); // AuthRequiredError: stale session
// after: force a fresh login when verify reports auth
try {
  await toutiaoAuthVerify(page);
} catch (e) {
  if (e.name === 'AuthRequiredError') {
    await clearCookies(page, 'mp.toutiao.com');
    await toutiaoLogin(page);
    await toutiaoAuthVerify(page);
  } else throw e;
}
Defensive patterns

Strategy: try-catch

Validate before calling

const cookies = await page.getCookies({ url: 'https://mp.toutiao.com' });
if (!cookies.some(c => c.name === 'sessionid' && c.value)) {
  throw new Error('sessionid missing — do a fresh login before verify');
}
function isValidToutiaoIdentity(id) {
  return !!id && typeof id.user_id === 'string' && id.user_id.length > 0;
}

Type guard

function isAuthProbeResult(probe) {
  return !!probe && typeof probe === 'object' && probe.kind === 'auth' && typeof probe.detail === 'string';
}

Try / catch

try {
  const identity = await toutiaoAuthVerify(page);
} catch (err) {
  if (err.name === 'AuthRequiredError') { // redirect-to-login or stale-session detail
    await clearSiteCookies(page, 'mp.toutiao.com');
    await toutiaoAuthLogin(page);
    return toutiaoAuthVerify(page);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling toutiao auth verify (or the poll path after the sessionid cookie exists) when: the URL matches /auth/page/login despite a sessionid cookie, or the probe finds no user_id in any state/cookie/DOM surface — typically an expired or invalidated session.

Common situations: Server-side session invalidated (logged in elsewhere, password change, TTL expiry) while the old sessionid cookie remains stored; Toutiao layout/A-B change renaming state keys and selectors so the probe can't find user info; anti-bot or error page served instead of the dashboard.

Related errors


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