
包含所有功能、完全可插拔的認證庫。四種流程。令牌儲存、自動刷新、跨進程鎖、整合系統金鑰管理。每個環節都幫你處理好,也都可以自由替換。
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、回調頁都設有 hooks,絕不硬編碼。點選 slot 就能看到配置自動改寫。
儲存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 用。