
全功能、完全可插拔的认证库。支持四种流程。包括令牌存储、刷新、跨进程锁和钥匙串集成。每个环节都有人为你处理,也都可以替换。
npm install cli-auth选择你的策略,底层细节都帮你包好了。
适合服务器、Docker shell 以及打不开浏览器的机器。
1import { createCliAuth, keyringStorage } from 'cli-auth';2import { Entry } from '@napi-rs/keyring';3 4const auth = createCliAuth({5 strategy: 'device-code',6 provider: {7 metadata: {8 deviceAuthorizationEndpoint: 'https://your-tenant.logto.app/oidc/device/auth',9 tokenEndpoint: 'https://your-tenant.logto.app/oidc/token',10 },11 },12 clientId: 'your-cli-client',13 storage: keyringStorage({ entry: new Entry('your-cli', 'tokens') }),14 scope: 'openid offline_access',15});16 17await auth.login({18 onAuthorization: ({ userCode, verificationUri }) => {19 console.log(`Visit ${verificationUri} and enter ${userCode}`);20 },21});22 23const accessToken = await auth.getToken();下面这些都是我们已经踩过的真实 bug、竞态以及规范陷阱。

存储、锁、fetch、回调页面都可自定义。没有硬编码。点一下插槽看看配置自动改写。
存储1import { createCliAuth } from 'cli-auth';2 3const auth = createCliAuth({4 strategy: 'authorization-code',5 provider: { /* ... */ },6 clientId: 'your-cli-client',7 storage: keyringStorage({ entry: new Entry('your-cli', 'tokens') }),8});只要是 OAuth 2.0 / OIDC 标准兼容的服务端都能用。直接设置 endpoint 就能跑。我们做的不止是给 Logto 用的。