{"record":{"id":"c461901eedb13ce2","repo":"jackwener/OpenCLI","slug":"auth-c46190","errorCode":null,"errorMessage":"auth","messagePattern":"auth","errorType":"exception","errorClass":"AuthRequiredError","httpStatus":null,"severity":"error","filePath":"clis/hf/auth.js","lineNumber":23,"sourceCode":"// documented /api/whoami-v2 endpoint (401 when anonymous) via a no-nav probe.\nconst WHOAMI_PROBE = `(async () => {\n  try {\n    const r = await fetch('/api/whoami-v2', { credentials: 'include', headers: { Accept: 'application/json' } });\n    if (r.status === 401 || r.status === 403) return { kind: 'auth', detail: 'HF /api/whoami-v2 HTTP ' + r.status };\n    if (!r.ok) return { kind: 'http', httpStatus: r.status };\n    const d = await r.json();\n    if (!d || !d.name || d.type === undefined) return { kind: 'auth', detail: 'HF /api/whoami-v2 has no name — anonymous' };\n    return { ok: true, username: String(d.name), fullname: String(d.fullname || ''), type: String(d.type || '') };\n  } catch (e) {\n    return { kind: 'exception', detail: String(e && e.message || e) };\n  }\n})()`;\n\nasync function verifyHfIdentity(page) {\n  await page.goto('https://huggingface.co/');\n  await page.wait(1);\n  const probe = await page.evaluate(WHOAMI_PROBE);\n  if (probe?.kind === 'auth') throw new AuthRequiredError('huggingface.co', probe.detail);\n  if (probe?.kind === 'http') throw new CommandExecutionError(`HTTP ${probe.httpStatus} from HF /api/whoami-v2`);\n  if (probe?.kind === 'exception') throw new CommandExecutionError(`HF whoami failed: ${probe.detail}`);\n  if (!probe?.ok) throw new CommandExecutionError(`Unexpected HF probe: ${JSON.stringify(probe)}`);\n  return { username: probe.username, fullname: probe.fullname, type: probe.type };\n}\n\nregisterSiteAuthCommands({\n  site: 'hf',\n  domain: 'huggingface.co',\n  loginUrl: 'https://huggingface.co/login',\n  columns: ['username', 'fullname', 'type'],\n  verify: verifyHfIdentity,\n  poll: async (page) => {\n    const probe = await page.evaluate(WHOAMI_PROBE);\n    if (!probe?.ok) throw new AuthRequiredError('huggingface.co', 'Waiting for Hugging Face login');\n    return { username: probe.username, fullname: probe.fullname, type: probe.type };\n  },\n});","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/hf/auth.js#L5-L41","documentation":"`verifyHfIdentity` in `clis/hf/auth.js` injects a whoami probe into a logged-in browser page on huggingface.co. When the probe reports `kind === 'auth'`, the user is not authenticated with Hugging Face, so the library throws `AuthRequiredError('huggingface.co', detail)`. It exists to fail fast with an actionable 'log in first' message instead of failing later on authenticated API calls.","triggerScenarios":"Running an `hf` command that needs identity verification while the browser session has no valid Hugging Face login (no session cookie, or expired session); being logged out or using a fresh/incognito browser profile; HF clearing or invalidating the session token server-side.","commonSituations":"CI or fresh containers where the browser profile was never logged in; session expiry after weeks of inactivity; logging out of huggingface.co manually between runs; cookie jars wiped by browser updates or privacy cleaners.","solutions":["Open https://huggingface.co/ in the browser the CLI drives and log in (create an account if needed)","Re-run the hf command after confirming you see your avatar/username on the homepage","If sessions keep dropping, check that cookies for huggingface.co are not being blocked/cleared by browser settings or extensions"],"exampleFix":"// before (not logged in) -> AuthRequiredError\nconst me = await verifyHfIdentity(page);\n// after: log in at https://huggingface.co/login in the controlled browser first,\n// then re-run\nconst me = await verifyHfIdentity(page);","handlingStrategy":"try-catch","validationCode":"// Check login state before running auth-dependent hf commands\nconst res = await fetch('https://huggingface.co/api/whoami-v2', {\n  headers: { Authorization: `Bearer ${process.env.HF_TOKEN}` },\n});\nif (!res.ok) {\n  throw new Error('Not authenticated with Hugging Face — log in at https://huggingface.co/login');\n}","typeGuard":"function isAuthedWhoami(probe) {\n  return probe != null && probe.ok === true && typeof probe.username === 'string';\n}","tryCatchPattern":"try {\n  const me = await verifyHfIdentity(page);\n} catch (e) {\n  if (e.name === 'AuthRequiredError') {\n    console.error('Log in to huggingface.co in the controlled browser, then retry.');\n    await page.goto('https://huggingface.co/login');\n  } else throw e;\n}","preventionTips":["Log in to huggingface.co in the same browser profile the CLI uses before running hf commands","Avoid incognito/fresh profiles that carry no session cookies","Check session validity periodically; HF sessions expire and privacy cleaners may wipe cookies","Persist an HF token and verify it with /api/whoami-v2 as a preflight in CI"],"tags":["auth-required","huggingface","authentication","browser-session"],"backgroundTag":"auth-required","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}