Files
jackwener__opencli/clis/rednote/auth.js
jakevin 9139baaef8 feat(auth): wire quickCheck into 50 adapters for auth status (#1880)
Adds the no-navigation `quickCheck` to each adapter's
registerSiteAuthCommands config so `opencli auth status` (PR #1879) resolves
login state in quick mode (CDP getCookies, no per-site goto) instead of
reporting `unknown`.

- quickCheck reuses each adapter's existing poll cookie gate (has<Site>Cookie),
  which is a logged-in-only, no-nav check returning boolean.
- Deliberately NOT wired (stay `unknown` in quick mode, available via --full):
  - gitee/hf/deepseek/quark/reuters/zsxq: no reliable logged-in cookie; they
    detect via no-nav fetch / localStorage / Bearer which need the site origin.
  - doubao/ke/coupang/manus: session cookie is present for anonymous users, so a
    cookie quickCheck would false-positive — `unknown` is more honest.

Live: auth status --site resolves logged_in/not_logged_in for cookie-gate sites
(v2ex/github/zhihu/claude/taobao/twitter/bilibili) in quick mode; excluded
sites report unknown. Audits new=0/new=0; suite 5054 passed.
2026-06-06 21:18:43 +08:00

53 lines
2.1 KiB
JavaScript

import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
async function hasRednoteSessionCookie(page) {
const cookies = await page.getCookies({ url: 'https://www.rednote.com' });
return cookies.some(c => c.name === 'web_session' && c.value);
}
async function verifyRednoteIdentity(page) {
if (!await hasRednoteSessionCookie(page)) {
throw new AuthRequiredError('rednote.com', 'Rednote web_session cookie missing');
}
await page.goto('https://www.rednote.com/explore');
await page.wait(2);
const probe = await page.evaluate(`
(() => {
const state = window.__INITIAL_STATE__;
if (!state?.user) {
return { kind: 'auth', detail: 'Rednote __INITIAL_STATE__.user missing' };
}
const loggedIn = state.user.loggedIn?._value;
const userInfo = state.user.userInfo?._value || {};
if (loggedIn !== true) {
return { kind: 'auth', detail: 'Rednote loggedIn._value=' + String(loggedIn) + ' — anonymous' };
}
const userId = String(userInfo.userId || userInfo.user_id || '');
const nickname = String(userInfo.nickname || userInfo.name || '');
if (!userId) {
return { kind: 'auth', detail: 'Rednote logged-in but userId missing — stale session' };
}
return { ok: true, user_id: userId, nickname };
})()
`);
if (probe?.kind === 'auth') throw new AuthRequiredError('rednote.com', probe.detail);
if (!probe?.ok) throw new CommandExecutionError(`Unexpected Rednote probe: ${JSON.stringify(probe)}`);
return { user_id: probe.user_id, nickname: probe.nickname };
}
registerSiteAuthCommands({
site: 'rednote',
domain: 'rednote.com',
loginUrl: 'https://www.rednote.com/explore',
columns: ['user_id', 'nickname'],
quickCheck: hasRednoteSessionCookie,
verify: verifyRednoteIdentity,
poll: async (page) => {
if (!await hasRednoteSessionCookie(page)) {
throw new AuthRequiredError('rednote.com', 'Waiting for Rednote web_session cookie');
}
return verifyRednoteIdentity(page);
},
});