{"record":{"id":"7b6b170c734b6ec7","repo":"jackwener/OpenCLI","slug":"pixiv-phpsessid-cookie-missing","errorCode":null,"errorMessage":"Pixiv PHPSESSID cookie missing","messagePattern":"Pixiv PHPSESSID cookie missing","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/pixiv/auth.js","lineNumber":14,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasPixivSessionCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://www.pixiv.net' });\n  // Anonymous PHPSESSID is a bare hash; logged-in form is `<userId>_<hash>`.\n  // Require the numeric uid prefix so the login poll doesn't navigate away\n  // from accounts.pixiv.net while the user is still signing in.\n  return cookies.some(c => c.name === 'PHPSESSID' && /^\\d+_/.test(c.value || ''));\n}\n\nasync function verifyPixivIdentity(page) {\n  if (!await hasPixivSessionCookie(page)) {\n    throw new AuthRequiredError('pixiv.net', 'Pixiv PHPSESSID cookie missing');\n  }\n  await page.goto('https://www.pixiv.net/');\n  await page.wait(2);\n  const probe = await page.evaluate(`(async () => {\n    try {\n      const meta = document.querySelector('meta[name=\"global-data\"]')?.getAttribute('content') || '';\n      let userData = null;\n      if (meta) { try { userData = JSON.parse(meta); } catch {} }\n      const u = userData?.userData;\n      if (u?.id) {\n        return { ok: true, user_id: String(u.id), name: String(u.name || u.account || '') };\n      }\n      const r = await fetch('/ajax/user/extra', { credentials: 'include', headers: { Accept: 'application/json' } });\n      if (r.status === 401 || r.status === 403) {\n        return { kind: 'auth', detail: 'Pixiv /ajax/user/extra HTTP ' + r.status };\n      }\n      if (!r.ok) return { kind: 'http', httpStatus: r.status };\n      const d = await r.json();","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/pixiv/auth.js#L1-L32","documentation":"verifyPixivIdentity first checks the browser context for a PHPSESSID cookie whose value has the logged-in form `<numeric userId>_...` (anonymous sessions are a bare hash). If no such cookie exists it throws AuthRequiredError, signalling the pixiv.net CLI needs an interactive login before it can verify identity.","triggerScenarios":"Running any pixiv auth-required command when the browser profile has no pixiv.net PHPSESSID cookie, or only an anonymous (non `\\d+_`) PHPSESSID — never logged in, cookies cleared, or login still in progress.","commonSituations":"Fresh machine/CI container without a persisted browser profile; user cleared cookies; headless run before completing the accounts.pixiv.net login flow; expired session where pixiv dropped the cookie entirely.","solutions":["Run the pixiv login flow (`opencli pixiv login`) and complete sign-in at accounts.pixiv.net.","Verify a `\\d+_...`-shaped PHPSESSID cookie exists for https://www.pixiv.net in the browser profile.","Persist/reuse the same browser profile directory between runs so the session cookie survives.","Re-login if the session expired — pixiv rotates PHPSESSID periodically."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://www.pixiv.net' });\nconst loggedIn = cookies.some(c => c.name === 'PHPSESSID' && /^\\d+_/.test(c.value || ''));\nif (!loggedIn) await runLoginFlow(); // e.g. opencli pixiv login","typeGuard":"function hasPixivSessionCookie(cookies) {\n  return cookies.some(c => c.name === 'PHPSESSID' && /^\\d+_/.test(c.value || ''));\n}","tryCatchPattern":"try {\n  await pixivWhoAmI();\n} catch (err) {\n  if (String(err.message).includes('PHPSESSID cookie missing')) {\n    await interactiveLogin('https://accounts.pixiv.net/login');\n    await pixivWhoAmI();\n  } else throw err;\n}","preventionTips":["Persist the browser profile directory so pixiv cookies survive across runs/CI.","Check the PHPSESSID shape (`\\d+_...`) before running auth-required pixiv commands.","Re-login proactively when sessions age out rather than mid-script."],"tags":["pixiv","auth","cookie","session"],"backgroundTag":"missing-auth-cookie","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}