{"record":{"id":"375cddb576c6d53b","repo":"jackwener/OpenCLI","slug":"grok-com","errorCode":null,"errorMessage":"grok.com","messagePattern":"grok\\.com","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/grok/auth.js","lineNumber":11,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasGrokSessionCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://grok.com' });\n  return cookies.some(c => c.name === '__Secure-next-auth.session-token' && c.value);\n}\n\nasync function verifyGrokIdentity(page) {\n  if (!await hasGrokSessionCookie(page)) {\n    throw new AuthRequiredError('grok.com', 'Grok __Secure-next-auth.session-token cookie missing');\n  }\n  await page.goto('https://grok.com/');\n  await page.wait(2);\n  const result = await page.evaluate(`(async () => {\n    try {\n      const res = await fetch('/api/auth/session', { credentials: 'include', headers: { 'Accept': 'application/json' } });\n      if (res.status === 401 || res.status === 403) {\n        return { kind: 'auth', detail: 'Grok /api/auth/session HTTP ' + res.status };\n      }\n      if (!res.ok) return { kind: 'http', httpStatus: res.status };\n      const d = await res.json();\n      const user = d && d.user;\n      if (!user || !user.id) {\n        return { kind: 'auth', detail: 'Grok /api/auth/session has no user — anonymous' };\n      }\n      return { ok: true, user_id: String(user.id), name: String(user.name || '') };\n    } catch (e) {\n      return { kind: 'exception', detail: String(e && e.message || e) };","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/grok/auth.js#L1-L29","documentation":"verifyGrokIdentity() (the grok site's auth 'whoami'/verify step) throws AuthRequiredError('grok.com', 'Grok __Secure-next-auth.session-token cookie missing') when the browser session's cookies for https://grok.com contain no non-empty __Secure-next-auth.session-token. That NextAuth session cookie is the library's quick, local check that the user is logged in to grok.com; without it every grok command would be anonymous. The error tells the user to perform the interactive site login flow before using grok commands.","triggerScenarios":"Calling any grok auth verify/whoami flow (or registerSiteAuthCommands login/poll path) when: (1) the persistent browser session was never logged in to grok.com; (2) the session cookie expired or was cleared (browser profile reset, cookie purge, logout elsewhere); (3) get_cookies for url https://grok.com returns cookies but none named '__Secure-next-auth.session-token' (e.g. only anonymous/CF cookies); (4) the cookie exists but with an empty value, which the hasGrokSessionCookie check rejects.","commonSituations":"Fresh environment/container where the persistent profile has no grok.com login; cookies expired after days/weeks since last login; running from a different machine or browser profile than the one used to log in; corporate cookie cleaners or incognito sessions wiping __Secure- cookies; grok.com logout invalidating the stored session.","solutions":["Run the grok site login flow (loginUrl https://grok.com/auth/sign-in, e.g. the registered `grok login` command) and complete sign-in interactively in the managed browser.","After logging in, re-run the auth verify/whoami command to confirm __Secure-next-auth.session-token is present and /api/auth/session returns a user.","If you log in manually, use the same persistent browser profile the CLI uses so the cookie lands in the right store.","Check you are not in an incognito/profile-wiping setup that discards __Secure- cookies between runs."],"exampleFix":"// before: verify against a session that was never logged in\nconst identity = await grokWhoami(); // AuthRequiredError\n// after: run the login flow first\nawait grokLogin(); // opens https://grok.com/auth/sign-in and polls until session cookie exists\nconst identity = await grokWhoami();","handlingStrategy":"validation","validationCode":"// Before invoking grok commands, check the session cookie in the persistent profile.\nconst cookies = await page.getCookies({ url: 'https://grok.com' });\nconst hasSession = cookies.some(c => c.name === '__Secure-next-auth.session-token' && c.value);\nif (!hasSession) {\n  await run('grok', 'login'); // interactive sign-in before any grok command\n}","typeGuard":null,"tryCatchPattern":"try {\n  const who = await run('grok', 'whoami');\n} catch (e) {\n  if (e instanceof AuthRequiredError) {\n    await run('grok', 'login');\n    return await run('grok', 'whoami');\n  }\n  throw e;\n}","preventionTips":["Run the whoami/verify command as a preflight before any grok automation batch.","Always use the same persistent browser profile for manual logins and CLI runs.","Re-login proactively when sessions are older than the NextAuth token lifetime.","Avoid incognito/cookie-wiping browser setups for the CLI's managed profile.","Catch AuthRequiredError and trigger the login flow instead of failing the whole batch."],"tags":["authentication","cookies","grok","login-required"],"backgroundTag":"missing-session-cookie","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}