{"record":{"id":"32489b68713a13bd","repo":"jackwener/OpenCLI","slug":"nowcoder-t-cookie-missing-anonymous","errorCode":null,"errorMessage":"Nowcoder t cookie missing (anonymous)","messagePattern":"Nowcoder t cookie missing \\(anonymous\\)","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/nowcoder/auth.js","lineNumber":41,"sourceCode":"    const r = await fetch('https://gw-c.nowcoder.com/api/sparta/user/profile/' + uid, {\n      credentials: 'include',\n      headers: { Accept: 'application/json' },\n    });\n    if (r.status === 401 || r.status === 403) return { kind: 'auth', detail: 'nowcoder profile HTTP ' + r.status };\n    if (!r.ok) return { kind: 'http', httpStatus: r.status };\n    const d = await r.json();\n    if (!d || !d.success || !d.data || !d.data.id) {\n      return { kind: 'auth', detail: 'nowcoder profile returned no user data (anonymous)' };\n    }\n    return { ok: true, user_id: String(d.data.id), nickname: String(d.data.nickname || '') };\n  } catch (e) {\n    return { kind: 'exception', detail: String(e && e.message || e) };\n  }\n})()`;\n\nasync function verifyNowcoderIdentity(page) {\n  if (!await hasNowcoderSessionCookie(page)) {\n    throw new AuthRequiredError('nowcoder.com', 'Nowcoder t cookie missing (anonymous)');\n  }\n  await page.goto('https://www.nowcoder.com/');\n  await page.wait(2);\n  const probe = await page.evaluate(WHOAMI_PROBE);\n  if (probe?.kind === 'auth') throw new AuthRequiredError('nowcoder.com', probe.detail);\n  if (probe?.kind === 'http') throw new CommandExecutionError(`HTTP ${probe.httpStatus} from nowcoder profile API`);\n  if (probe?.kind === 'exception') throw new CommandExecutionError(`Nowcoder whoami failed: ${probe.detail}`);\n  if (!probe?.ok) throw new CommandExecutionError(`Unexpected nowcoder probe: ${JSON.stringify(probe)}`);\n  return { user_id: probe.user_id, nickname: probe.nickname };\n}\n\nregisterSiteAuthCommands({\n  site: 'nowcoder',\n  domain: 'nowcoder.com',\n  loginUrl: 'https://www.nowcoder.com/login',\n  columns: ['user_id', 'nickname'],\n  verify: verifyNowcoderIdentity,\n  poll: async (page) => {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/nowcoder/auth.js#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"Fresh browser profile with no login; cookies wiped by cleanup tooling; session expiry; running in CI/headless without having completed `nowcoder login`.","solutions":["Run the nowcoder login command (opens https://www.nowcoder.com/login) to establish the `t` cookie","Log in manually in the browser profile the CLI uses","Check that the correct browser profile is being used (cookies are per-profile)","Verify the cookie survives: page.getCookies({url:'https://www.nowcoder.com'}) should list a non-empty `t`"],"exampleFix":"// before\nconst cookies = await page.getCookies({ url: 'https://www.nowcoder.com' });\nif (!cookies.some(c => c.name === 't' && c.value)) throw new Error('not logged in');\n// after\nconst cookies = await page.getCookies({ url: 'https://www.nowcoder.com' });\nif (!cookies.some(c => c.name === 't' && c.value)) {\n  throw new AuthRequiredError('nowcoder.com', 'Nowcoder t cookie missing (anonymous)');\n}","handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://www.nowcoder.com' });\nconst loggedIn = cookies.some(c => c.name === 't' && c.value);\nif (!loggedIn) {\n  throw new AuthRequiredError('nowcoder.com', 'Nowcoder t cookie missing (anonymous)');\n}","typeGuard":"function hasNowcoderSession(cookies) {\n  return Array.isArray(cookies) && cookies.some(c => c.name === 't' && typeof c.value === 'string' && c.value.length > 0);\n}","tryCatchPattern":"try {\n  await nowcoderWhoami(page);\n} catch (e) {\n  if (e instanceof AuthRequiredError && /t cookie missing/.test(e.message)) {\n    await nowcoderLogin(page);\n    return nowcoderWhoami(page);\n  }\n  throw e;\n}","preventionTips":["Run `nowcoder login` before any nowcoder command in fresh environments","Check for the `t` cookie in your scripts before making authed calls","Avoid cookie-clearing tools on the browser profile the CLI uses","Re-login periodically; the `t` cookie expires like any session token"],"tags":["auth","cookies","session-missing","nowcoder"],"backgroundTag":"missing-session-cookie","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}