jackwener/OpenCLI · error · AuthRequiredError

Nowcoder t cookie missing (anonymous)

Error message

Nowcoder t cookie missing (anonymous)

What it means

verifyNowcoderIdentity requires the nowcoder.com session cookie named `t`. When page.getCookies for https://www.nowcoder.com contains no non-empty `t` cookie, the user is effectively anonymous and an AuthRequiredError is thrown with detail 'Nowcoder t cookie missing (anonymous)'. The library cannot resolve user identity without the logged-in token.

Source

Thrown at clis/nowcoder/auth.js:41

    const r = await fetch('https://gw-c.nowcoder.com/api/sparta/user/profile/' + uid, {
      credentials: 'include',
      headers: { Accept: 'application/json' },
    });
    if (r.status === 401 || r.status === 403) return { kind: 'auth', detail: 'nowcoder profile HTTP ' + r.status };
    if (!r.ok) return { kind: 'http', httpStatus: r.status };
    const d = await r.json();
    if (!d || !d.success || !d.data || !d.data.id) {
      return { kind: 'auth', detail: 'nowcoder profile returned no user data (anonymous)' };
    }
    return { ok: true, user_id: String(d.data.id), nickname: String(d.data.nickname || '') };
  } catch (e) {
    return { kind: 'exception', detail: String(e && e.message || e) };
  }
})()`;

async function verifyNowcoderIdentity(page) {
  if (!await hasNowcoderSessionCookie(page)) {
    throw new AuthRequiredError('nowcoder.com', 'Nowcoder t cookie missing (anonymous)');
  }
  await page.goto('https://www.nowcoder.com/');
  await page.wait(2);
  const probe = await page.evaluate(WHOAMI_PROBE);
  if (probe?.kind === 'auth') throw new AuthRequiredError('nowcoder.com', probe.detail);
  if (probe?.kind === 'http') throw new CommandExecutionError(`HTTP ${probe.httpStatus} from nowcoder profile API`);
  if (probe?.kind === 'exception') throw new CommandExecutionError(`Nowcoder whoami failed: ${probe.detail}`);
  if (!probe?.ok) throw new CommandExecutionError(`Unexpected nowcoder probe: ${JSON.stringify(probe)}`);
  return { user_id: probe.user_id, nickname: probe.nickname };
}

registerSiteAuthCommands({
  site: 'nowcoder',
  domain: 'nowcoder.com',
  loginUrl: 'https://www.nowcoder.com/login',
  columns: ['user_id', 'nickname'],
  verify: verifyNowcoderIdentity,
  poll: async (page) => {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run the nowcoder login command (opens https://www.nowcoder.com/login) to establish the `t` cookie
  2. Log in manually in the browser profile the CLI uses
  3. Check that the correct browser profile is being used (cookies are per-profile)
  4. Verify the cookie survives: page.getCookies({url:'https://www.nowcoder.com'}) should list a non-empty `t`

Example fix

// before
const cookies = await page.getCookies({ url: 'https://www.nowcoder.com' });
if (!cookies.some(c => c.name === 't' && c.value)) throw new Error('not logged in');
// after
const cookies = await page.getCookies({ url: 'https://www.nowcoder.com' });
if (!cookies.some(c => c.name === 't' && c.value)) {
  throw new AuthRequiredError('nowcoder.com', 'Nowcoder t cookie missing (anonymous)');
}
Defensive patterns

Strategy: validation

Validate before calling

const cookies = await page.getCookies({ url: 'https://www.nowcoder.com' });
const loggedIn = cookies.some(c => c.name === 't' && c.value);
if (!loggedIn) {
  throw new AuthRequiredError('nowcoder.com', 'Nowcoder t cookie missing (anonymous)');
}

Type guard

function hasNowcoderSession(cookies) {
  return Array.isArray(cookies) && cookies.some(c => c.name === 't' && typeof c.value === 'string' && c.value.length > 0);
}

Try / catch

try {
  await nowcoderWhoami(page);
} catch (e) {
  if (e instanceof AuthRequiredError && /t cookie missing/.test(e.message)) {
    await nowcoderLogin(page);
    return nowcoderWhoami(page);
  }
  throw e;
}

Prevention

When it happens

Trigger: Running `nowcoder` auth-verify, whoami, notifications, or papers commands when no login has been performed, after the `t` cookie expired/was cleared, or when the browser profile used is not the logged-in one.

Common situations: Fresh browser profile with no login; cookies wiped by cleanup tooling; session expiry; running in CI/headless without having completed `nowcoder login`.

Related errors


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