@atcute/password-session

中文文档
1.0.2发布于 2026/07/160BSDES Module近 30 天下载 1748

password-based session management for AT Protocol

原始使用文档
文档目录8 节
  1. usage
  2. login
  3. URL shorthand
  4. resuming sessions
  5. cached session with credential fallback
  6. lazy construction
  7. cleanup
  8. callbacks

@atcute/password-session

password-based session management for AT Protocol services. manages access/refresh token lifecycle, automatic refresh on 401, and session persistence via callbacks.

for browser-based applications, prefer OAuth-based authentication. when using password auth, use app passwords rather than main account credentials.

npm install @atcute/password-session @atcute/client @atcute/bluesky

usage

login

import { Client, ok } from '@atcute/client';
import { PasswordSession } from '@atcute/password-session';

import type {} from '@atcute/bluesky';

const session = await PasswordSession.login(
    { service: 'https://bsky.social', identifier: 'you.bsky.social', password: 'your-app-password' },
    {
        onUpdate(data) {
            // called on login and token refresh — persist the session
            localStorage.setItem('session', JSON.stringify(data));
        },
        onDelete(data) {
            // called on logout or session invalidation — clean up
            localStorage.removeItem('session');
        },
    },
);

const rpc = new Client({ handler: session });

const data = await ok(rpc.get('com.atproto.server.getSession'));
console.log(data.did);

URL shorthand

for bots and scripts, use URL shorthand with await using for automatic logout:

await using session = await PasswordSession.login('https://handle:[email protected]');
const rpc = new Client({ handler: session });

resuming sessions

resume a persisted session without re-entering credentials:

const saved = localStorage.getItem('session');
if (saved) {
    const session = await PasswordSession.resume(JSON.parse(saved), {
        onUpdate(data) {
            localStorage.setItem('session', JSON.stringify(data));
        },
        onDelete(data) {
            localStorage.removeItem('session');
        },
    });
    const rpc = new Client({ handler: session });
}

cached session with credential fallback

for bots with both stored credentials and cached sessions, login() can try the cached session first and fall back to fresh authentication:

const session = await PasswordSession.login('https://handle:[email protected]', {
    session: loadFromDisk(),
    onUpdate(data) {
        saveToDisk(data);
    },
});

lazy construction

if you don't need upfront validation, construct directly — tokens refresh lazily on 401:

const session = new PasswordSession(savedData, { onUpdate, onDelete });
const rpc = new Client({ handler: session });

cleanup

delete an orphaned session server-side without resuming:

await PasswordSession.delete(savedData);

callbacks

callbackwhensession state
onUpdatelogin succeeds, tokens refresh successfullyactive (updated)
onUpdateFailuretoken refresh fails transiently (network)active (preserved)
onDeletelogout succeeds, session invalidateddestroyed
onDeleteFailurelogout fails transiently (network)active (preserved)

all callbacks receive this: PasswordSession context and must not throw.

版本与发布记录

@atcute/password-session 共发布 5 个版本,首次发布于 2026/02/24;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.0.2latest2026/07/163 个月前
1.0.12026/06/304 个月前
1.0.02026/05/096 个月前
0.1.12026/05/086 个月前
0.1.02026/02/248 个月前

关于 @atcute/password-session 的常见问题

@atcute/password-session 是什么?

password-based session management for AT Protocol

如何安装 @atcute/password-session?

在项目目录运行 npm install @atcute/password-session,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@atcute/identity(^2.0.0)、@atcute/lexicons(^2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atcute/password-session 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/atcute__password-session 是否存在。

@atcute/password-session 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@atcute/password-session 可以商用吗?

@atcute/password-session 使用 0BSD 许可。几乎没有限制:可自由使用、修改和分发,连保留版权声明也不要求。以上为概括,不构成法律意见,请以许可原文为准。

@atcute/password-session 的下载量有多少?

根据 npm 官方下载统计,@atcute/password-session 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,748 次,日均约 58 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atcute/password-session 还在维护吗?

最新版本 1.0.2 发布于 2026/07/16(3 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/02/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。