{"record":{"id":"df9e7697ea752e0c","repo":"jackwener/OpenCLI","slug":"xiaoe-xiaoeid-b-user-token-cookie-missing-anonym","errorCode":null,"errorMessage":"Xiaoe XIAOEID/b_user_token cookie missing — anonymous","messagePattern":"Xiaoe XIAOEID/b_user_token cookie missing — anonymous","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/xiaoe/auth.js","lineNumber":11,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasXiaoeAdminCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://admin.xiaoe-tech.com' });\n  return cookies.some(c => (c.name === 'XIAOEID' || c.name === 'b_user_token') && c.value);\n}\n\nasync function verifyXiaoeIdentity(page) {\n  if (!await hasXiaoeAdminCookie(page)) {\n    throw new AuthRequiredError('xiaoe-tech.com', 'Xiaoe XIAOEID/b_user_token cookie missing — anonymous');\n  }\n  await page.goto('https://admin.xiaoe-tech.com/t/account/muti_index');\n  await page.wait(3);\n  const finalUrl = await page.evaluate(`location.href`);\n  if (/login|signin|#\\/wx$/.test(String(finalUrl || ''))) {\n    throw new AuthRequiredError('xiaoe-tech.com', `Xiaoe admin page redirected to login: ${finalUrl}`);\n  }\n  const cookies = await page.getCookies({ url: 'https://admin.xiaoe-tech.com' });\n  const xiaoeId = cookies.find(c => c.name === 'XIAOEID')?.value || '';\n  const unionId = cookies.find(c => c.name === 'unionid')?.value || '';\n  const probe = await page.evaluate(`\n    (() => {\n      const bodyText = document.body?.innerText || '';\n      if (/微信扫码登录|手机号登录|登录小鹅通/.test(bodyText)) {\n        return { isLoginPage: true };\n      }\n      const nick = document.querySelector('.user-name, .nickname, [class*=\"userName\"], [class*=\"user-info\"]')?.innerText?.trim() || '';\n      return { isLoginPage: false, domNick: nick };","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/xiaoe/auth.js#L1-L29","documentation":"verifyXiaoeIdentity first checks the browser context for a non-empty XIAOEID or b_user_token cookie scoped to admin.xiaoe-tech.com. If neither cookie exists, the session is anonymous and the library throws AuthRequiredError so the caller can run the interactive login flow instead of scraping an unauthenticated page.","triggerScenarios":"Calling verifyXiaoeIdentity (or any xiaoe command that invokes quickCheck/verify) when page.getCookies({url:'https://admin.xiaoe-tech.com'}) returns no XIAOEID and no b_user_token cookie, or both have empty values.","commonSituations":"Fresh browser profile that never logged into the Xiaoe admin; cookies cleared by cleanup jobs or incognito sessions; wrong cookie domain after Xiaoe changed admin domains; running headless automation on a machine that was never logged in.","solutions":["Run the xiaoe login flow (registerSiteAuthCommands registers it) to obtain the admin cookies, then retry.","Open https://admin.xiaoe-tech.com/ manually in the same browser profile and confirm you are logged in.","Verify the browser profile / user-data-dir used by page is the one holding the session cookies.","Check that page.getCookies targets the exact URL https://admin.xiaoe-tech.com (cookie scoping is URL-based)."],"exampleFix":"// before\nawait verifyXiaoeIdentity(page); // throws if anonymous\n// after\nif (!(await hasXiaoeAdminCookie(page))) {\n  await runXiaoeLogin(page); // interactive login first\n}\nawait verifyXiaoeIdentity(page);","handlingStrategy":"validation","validationCode":"const cookies = await page.getCookies({ url: 'https://admin.xiaoe-tech.com' });\nconst authed = cookies.some(c => (c.name === 'XIAOEID' || c.name === 'b_user_token') && c.value);\nif (!authed) await runXiaoeLogin(page);","typeGuard":"function hasXiaoeCookie(cs) {\n  return Array.isArray(cs) && cs.some(c => (c.name === 'XIAOEID' || c.name === 'b_user_token') && !!c.value);\n}","tryCatchPattern":"try {\n  await verifyXiaoeIdentity(page);\n} catch (e) {\n  if (e instanceof AuthRequiredError) await runXiaoeLogin(page);\n  else throw e;\n}","preventionTips":["Always run the login flow before automated xiaoe scraping.","Use a persistent browser profile so cookies survive restarts.","Quick-check cookies with hasXiaoeAdminCookie before any admin-domain navigation."],"tags":["auth","cookies","xiaoe","browser-automation"],"backgroundTag":"missing-auth-cookie","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}