{"record":{"id":"65e480a240b7865e","repo":"jackwener/OpenCLI","slug":"google-session-cookies-are-missing","errorCode":null,"errorMessage":"Google session cookies are missing","messagePattern":"Google session cookies are missing","errorType":"error_code","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/gmail/auth.js","lineNumber":13,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\nimport { GMAIL_HOST, GMAIL_ORIGIN, unwrapBrowserResult } from './utils.js';\n\nasync function hasGoogleSession(page) {\n  const cookies = await page.getCookies({ url: GMAIL_ORIGIN });\n  const names = new Set(cookies.map((cookie) => cookie.name));\n  return names.has('SID') || names.has('__Secure-1PSID') || names.has('SAPISID');\n}\n\nasync function verifyGmailIdentity(page) {\n  if (!await hasGoogleSession(page)) {\n    throw new AuthRequiredError(GMAIL_HOST, 'Google session cookies are missing');\n  }\n  await page.goto(`${GMAIL_ORIGIN}/mail/u/0/#inbox`);\n  await page.sleep(2);\n  const result = unwrapBrowserResult(await page.evaluate(`(() => {\n    const account = Array.from(document.querySelectorAll('a[aria-label], button[aria-label]'))\n      .map((node) => String(node.getAttribute('aria-label') || '').trim())\n      .find((label) => /@/.test(label) && /(google account|google 帐号|google 账号)/i.test(label));\n    if (!account) {\n      const login = document.querySelector('a[href*=\"accounts.google.com/ServiceLogin\"], input[type=\"email\"]');\n      return login\n        ? { kind: 'auth', detail: 'Gmail shows a Google sign-in surface' }\n        : { kind: 'shape', detail: 'Gmail account control was not found' };\n    }\n    const email = account.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}/i)?.[0] || '';\n    const beforeEmail = email ? account.slice(0, account.indexOf(email)) : account;\n    const name = beforeEmail\n      .replace(/^.*?(?:google account|google 帐号|google 账号)\\s*[:：]?\\s*/i, '')\n      .replace(/[,(（]\\s*$/, '')","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/gmail/auth.js#L1-L31","documentation":"verifyGmailIdentity first checks hasGoogleSession(page), which requires at least one of the SID, __Secure-1PSID, or SAPISID cookies for the Google domain. When none are present it throws AuthRequiredError('gmail', 'Google session cookies are missing'), meaning the browser has no authenticated Google/Gmail session.","triggerScenarios":"Any gmail command's verify/poll path calls verifyGmailIdentity on a browser context whose Google cookie jar lacks SID, __Secure-1PSID, and SAPISID — i.e. never logged into Google in that profile, or the session was signed out/expired.","commonSituations":"Fresh or incognito browser profile; Google session expired (these cookies rotate/expire); user logged out of Google elsewhere causing global sign-out; cookie-blocking extensions or disabled cookies; wrong user-data-dir so cookies from the logged-in profile are not loaded.","solutions":["Log into Google/Gmail in the browser session, then re-run the command","Point the automation at the persistent profile (user-data-dir) that already holds Google cookies","Clear stale cookies and perform a fresh login if Google signed the session out globally","Disable cookie-blocking extensions or enable cookies for google.com/gmail.com"],"exampleFix":"// before\nconst identity = await verifyGmailIdentity(page); // throws without cookies\n// after\nif (!await hasGoogleSession(page)) {\n  await performGoogleLogin(page, { account: 0 }); // interactive login\n}\nconst identity = await verifyGmailIdentity(page);","handlingStrategy":"validation","validationCode":"const cookies = await page.cookies('https://mail.google.com');\nconst names = new Set(cookies.map(c => c.name));\nif (!(['SID','__Secure-1PSID','SAPISID'].some(n => names.has(n)))) {\n  throw new Error('No Google session — log into Gmail first');\n}","typeGuard":"function hasGoogleSessionCookies(cookies) {\n  const names = new Set((cookies ?? []).map(c => c.name));\n  return names.has('SID') || names.has('__Secure-1PSID') || names.has('SAPISID');\n}","tryCatchPattern":"try {\n  const identity = await verifyGmailIdentity(page);\n} catch (e) {\n  if (e instanceof AuthRequiredError && /cookies are missing/.test(e.message)) {\n    await interactiveGoogleLogin(page);\n    return verifyGmailIdentity(page);\n  }\n  throw e;\n}","preventionTips":["Use a persistent profile that remains signed into Google","Check SID/SAPISID cookies before each gmail command run","Re-login proactively; Google can revoke sessions globally","Disable cookie blockers for google.com","Never share user-data-dir across unrelated automation jobs"],"tags":["auth","cookies","gmail","google","session"],"backgroundTag":"auth-required-session-missing","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}