{"record":{"id":"71c86446e344dcf9","repo":"jackwener/OpenCLI","slug":"ke-lianjia-token-cookie-missing-anonymous","errorCode":null,"errorMessage":"Ke lianjia_token cookie missing — anonymous","messagePattern":"Ke lianjia_token cookie missing — anonymous","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/ke/auth.js","lineNumber":11,"sourceCode":"import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';\nimport { registerSiteAuthCommands } from '../_shared/site-auth.js';\n\nasync function hasKeSessionCookie(page) {\n  const cookies = await page.getCookies({ url: 'https://www.ke.com' });\n  return cookies.some(c => c.name === 'lianjia_token' && c.value);\n}\n\nasync function verifyKeIdentity(page) {\n  if (!await hasKeSessionCookie(page)) {\n    throw new AuthRequiredError('ke.com', 'Ke lianjia_token cookie missing — anonymous');\n  }\n  await page.goto('https://www.ke.com/');\n  await page.wait(2);\n  const probe = await page.evaluate(`\n    (() => {\n      const loginBtn = document.querySelector('.btn-login, a[class*=actLoginBtn], .login-btn');\n      if (loginBtn && /登录|登陆/.test(loginBtn.innerText || '')) {\n        return { kind: 'auth', detail: 'Ke shows 登录 button — anonymous session' };\n      }\n      // 2026-08 贝壳新版 SSR：用户名在 .typeShowUser（脱敏手机号如 15****93），\n      // 旧锚点 .userNick/.user-name/.myInfo 已下线，故把 .typeShowUser 提到最前。\n      const el = document.querySelector('.typeShowUser a span, .typeShowUser a, .userNick, .user-name, .myInfo a, [class*=userNick]');\n      const username = (el?.innerText || '').trim();\n      if (!username) {\n        return { kind: 'auth', detail: 'Ke no user-name DOM anchor — anonymous or SSR failed' };\n      }\n      return { ok: true, username };\n    })()","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/ke/auth.js#L1-L29","documentation":"verifyKeIdentity checks the browser session for a non-empty `lianjia_token` cookie on ke.com before probing the DOM. If the cookie is absent it throws AuthRequiredError meaning the browser is anonymous — the user must log into ke.com in the managed browser session before CLI commands can authenticate.","triggerScenarios":"Calling any `ke` site command (or `ke auth verify`) when the browser profile has never logged into ke.com; cookies were cleared; the session cookie expired and was dropped; page.getCookies returns no matching `lianjia_token` cookie.","commonSituations":"Fresh browser profile / new machine; user logged out in the shared browser; incognito context without persisted cookies; Lianjia/Beike invalidated old tokens server-side.","solutions":["Run the site's login command to open https://clogin.ke.com/login/?service=https%3A%2F%2Fwww.ke.com and complete login in the browser","Verify the cookie exists: in the browser devtools check www.ke.com cookies for `lianjia_token`","Re-run verify after login; if it still fails, clear stale cookies and log in again"],"exampleFix":"// before (CLI)\n$ opencli ke house list  # AuthRequiredError: lianjia_token missing\n// after\n$ opencli ke auth login  # complete login in browser, then retry","handlingStrategy":"try-catch","validationCode":"const cookies = await page.getCookies({ url: 'https://www.ke.com' });\nconst hasSession = cookies.some(c => c.name === 'lianjia_token' && c.value);\nif (!hasSession) {\n  console.log('Not logged into ke.com — run `opencli ke auth login` first');\n  process.exit(2);\n}","typeGuard":null,"tryCatchPattern":"import { AuthRequiredError } from '@jackwener/opencli/errors';\ntry {\n  await keCommand(args);\n} catch (e) {\n  if (e instanceof AuthRequiredError) {\n    console.error('ke.com login required. Run: opencli ke auth login');\n    process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Run `ke auth verify` before long batch jobs","Persist a dedicated browser profile so cookies survive restarts","Check cookie expiry and re-login proactively on a schedule","Catch AuthRequiredError uniformly across site commands and print the login hint"],"tags":["auth","session","browser","ke"],"backgroundTag":"auth-session-required","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}