@ai-matrx/auth

中文文档
0.4.19发布于 2026/10/28MITTypeScriptESM + CJS近 30 天下载 4.3万

AI Matrx sign-in, session and organizations for every app: sign in/out, session storage and refresh per platform (browser, Next.js, extension, desktop, Node), the current person, their organizations and favourites, the one active organization (held reques

原始使用文档

@ai-matrx/auth

Sign-in, session and organizations for every AI Matrx app — the small package at the top of the app. Framework-free core; /react and /next sections; pay only for the sections you import.

import { createAuthClient, signInWithPassword, sessionCredentials, signOut } from "@ai-matrx/auth";
import { createActiveOrganization, readMemberships } from "@ai-matrx/auth/organizations";

const supabase = createAuthClient({ url: "https://db.matrxserver.com", key: PUBLISHABLE_KEY });
const { person } = await signInWithPassword(supabase, { email, password });
const active = createActiveOrganization({ client: supabase });
const orgId = await active.ensureOrgId();           // held until the ladder answers
const credentials = sessionCredentials(supabase);   // hand to @ai-matrx/api's createApi
SectionWhat
@ai-matrx/authsign-in/out, session, refresh, sessionCredentials, storage per platform, createAuthClient
/organizationsmemberships, favorites, the active-organization ladder and createActiveOrganization
/credentialsthe five credential kinds and the one credential → header mapping
/session-identitywho is acting, live from the session
/cookiesthe domain-wide auth cookie and the active-organization cookie
/nextevery Next.js Supabase client (browser, RSC, route, middleware)
/browserthe framework-free browser Supabase client over the shared auth cookie (non-Next apps)
/reactuseSession, useActiveOrganization, useSessionIdentity
/nodefileStorage — a script's session survives between runs
/testingfake sessions for tests

Session storage: browser browserStorage(), extension extensionStorage(), desktop keyValueStorage(store), Node fileStorage(path) or memoryStorage(), Next.js /next.

The active organization is never a list filter: reads show everything the person can see; the active organization is only where new things are saved and which organization a server call runs in.

Sections added in 0.2.0 — one entry point each

NeedImport
Who is calling (local claims; browser de-dup)getClaimsUser, createClaimsCache from @ai-matrx/auth
Server identity in a Next appcreateServerAuth({ createClient, supabaseUrl, cache }) from @ai-matrx/auth/next
API route caller (Bearer or cookie)verifyRequest(request, { url, publishableKey, cookieClient }) from @ai-matrx/auth/node (or /next)
Server-only secret-key clientcreateServiceClient({ url, secretKey }) from @ai-matrx/auth/node (or /next)
"Needs an organization" refusalisOrganizationRequiredError, organizationRequired, organizationRequiredResponse from @ai-matrx/auth/organizations
Sign in with AI Matrx (extension, desktop, CLI)createMatrxOAuth(config, { storage, openAuthorization }) from @ai-matrx/auth/matrx-oauth
Is this person an admin (their own admin.admins row; admin + level / not_admin / unknown)readAdminStatus(client, userId) from @ai-matrx/auth/admin
Decode a JWT segment (base64url → bytes / UTF-8 text; no signature check)base64UrlToBytes, base64UrlToText from @ai-matrx/auth/jwt (dependency-free)

@ai-matrx/auth/spa — a Vite SPA that holds its own token

For a host that is NOT a Next/cookie app (admin dashboard, Workflow Studio): sign-in arrives as a one-time handoff code from aidream's OAuth hop, the access token lives in localStorage, and a signed-in aimatrx.com session on the same apex refreshes it through the session bridge.

import { sendMatrxRequest } from "@ai-matrx/api";
import { createSpaSession, createSpaSupabase, adminWhoamiVerifier } from "@ai-matrx/auth/spa";

export const session = createSpaSession({
  request: sendMatrxRequest,
  onBeforeTokenChange: (prev, next) => transitionQueryIdentity(prev, next),
  onTokenChange: () => refreshOrganizationIdentity(),
  bridge: { verifyToken: adminWhoamiVerifier(AIDREAM_API_URL) }, // dashboard only: admin gate
});
export const supabase = createSpaSupabase({ url, publishableKey, session, headers: { "x-matrx-admin-lane": "1" } });

Callback route: classifyCallback(location.search) -> exchangeHandoff(api, handoff, sendMatrxRequest) -> session.setToken(token). Pass the same request to reportTokenInQuery(api, params, where, sendMatrxRequest). The request is required: @ai-matrx/auth/spa does not make network calls through global fetch. Options: tokenKey (default matrx_admin_token), storage, loginPath (/login), navigate, bridge.sessionTokenUrl, bridge.sameSiteOnly (true), bridge.attemptLimit (3) / attemptWindowMs (20 000).

The active organization: createSpaOrganizationContext({ legacyStorageKey, apexDomains, tokenKey, serverUrl, db, sendRequest, bindOrganizationHeader, onIdentityChanged }) returns withOrganizationHeader, peekOrganizationId, chooseOrganization, listMemberOrganizations, the cookie and the rest; pass sendMatrxRequest and applyOrganizationContextHeader from @ai-matrx/api.

版本与发布记录

@ai-matrx/auth 共发布 43 个版本,首次发布于 2026/08/30;过去 12 个月发布了 43 个版本。

版本发布日期距今
0.4.19latest2026/10/288 天前
0.4.182026/10/2710 天前
0.4.172026/10/2511 天前
0.4.162026/10/2512 天前
0.4.152026/10/2413 天前
0.4.142026/10/2313 天前

关于 @ai-matrx/auth 的常见问题

@ai-matrx/auth 是什么?

AI Matrx sign-in, session and organizations for every app: sign in/out, session storage and refresh per platform (browser, Next.js, extension, desktop, Node), the current person, their organizations and favourites, the one active organization (held reques

如何安装 @ai-matrx/auth?

在项目目录运行 npm install @ai-matrx/auth,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@supabase/supabase-js(>=2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ai-matrx/auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ai-matrx/auth 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ai-matrx/auth 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ai-matrx/auth 可以商用吗?

@ai-matrx/auth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ai-matrx/auth 的下载量有多少?

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

@ai-matrx/auth 还在维护吗?

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