{"record":{"id":"686fea93bed902e3","repo":"jackwener/OpenCLI","slug":"facebook-c-user-cookie-missing-anonymous-session","errorCode":null,"errorMessage":"Facebook c_user cookie missing — anonymous session","messagePattern":"Facebook c_user cookie missing — anonymous session","errorType":"error_code","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/facebook/auth.js","lineNumber":11,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasFacebookCUserCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://www.facebook.com' });\n  return cookies.some(c => c.name === 'c_user' && c.value);\n}\n\nasync function verifyFacebookIdentity(page) {\n  if (!await hasFacebookCUserCookie(page)) {\n    throw new AuthRequiredError('www.facebook.com', 'Facebook c_user cookie missing — anonymous session');\n  }\n  const cookies = await page.getCookies({ url: 'https://www.facebook.com' });\n  const cUser = cookies.find(c => c.name === 'c_user')?.value || '';\n  await page.goto('https://www.facebook.com/me');\n  await page.wait(2);\n  const finalUrl = await page.evaluate(`location.href`);\n  const vanityMatch = String(finalUrl || '').match(/facebook\\.com\\/([^/?#]+)\\/?(?:$|[?#])/);\n  const vanity = vanityMatch?.[1] || '';\n  if (!vanity || vanity === 'login.php' || vanity === 'checkpoint') {\n    throw new AuthRequiredError('www.facebook.com', `Facebook /me redirected to ${finalUrl} — logged out or in checkpoint`);\n  }\n  return {\n    user_id: String(cUser),\n    vanity: String(vanity),\n    profile_url: `https://www.facebook.com/${vanity}/`,\n  };\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/facebook/auth.js#L1-L29","documentation":"verifyFacebookIdentity checks the Puppeteer page's cookies for www.facebook.com before scraping the profile. If no non-empty c_user cookie exists, the session is anonymous (logged out), so it throws AuthRequiredError to signal that interactive login is required. c_user is Facebook's logged-in user-id cookie; without it, /me scraping would only hit the login page.","triggerScenarios":"Calling verifyFacebookIdentity on a page whose getCookies('https://www.facebook.com') contains no cookie named 'c_user' with a non-empty value — i.e. the browser profile was never logged in, or the session was logged out.","commonSituations":"Fresh/empty browser profile used for the quick-check flow; Facebook invalidated the session server-side; cookies were cleared between runs; automation run headless without completing the interactive login.","solutions":["Run the interactive login flow (login.php via the CLI's login command) and complete Facebook login so a c_user cookie is set.","Verify cookies exist: page.getCookies({url:'https://www.facebook.com'}) should include c_user with a non-empty value.","Persist/reuse a browser profile directory so the logged-in session survives restarts.","Re-login if Facebook invalidated the session (password change, security checkpoint, remote logout)."],"exampleFix":"// before\nconst identity = await verifyFacebookIdentity(page);\n\n// after\nif (!(await hasFacebookCUserCookie(page))) {\n  await runInteractiveLogin(page); // opens login.php and waits for c_user\n}\nconst identity = await verifyFacebookIdentity(page);","handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://www.facebook.com' });\nconst loggedIn = cookies.some(c => c.name === 'c_user' && c.value);\nif (!loggedIn) await runInteractiveLogin(page);","typeGuard":"function hasCUser(cookies) {\n  return Array.isArray(cookies) && cookies.some(c => c.name === 'c_user' && typeof c.value === 'string' && c.value.length > 0);\n}","tryCatchPattern":"try {\n  const identity = await verifyFacebookIdentity(page);\n} catch (err) {\n  if (err instanceof AuthRequiredError) {\n    await performLogin(page); // interactive login.php flow\n  } else {\n    throw err;\n  }\n}","preventionTips":["Always run the quick check (hasFacebookCUserCookie) before scraping calls.","Persist the browser profile so the logged-in session survives restarts.","Re-login proactively when sessions age out instead of letting scrapes fail.","Never share cookie state across machines; Facebook invalidates suspicious sessions."],"tags":["authentication","cookies","facebook","browser-automation"],"backgroundTag":"auth-session-missing","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}