@fluixi/auth

中文文档
1.0.0-alpha.108发布于 2026/11/07MITTypeScriptESM + CJS近 30 天下载 2297

Server-first auth for Fluixi — native bindings over Better Auth: authMiddleware, requireUser/Role/Permission (request-context, no token parsing), SSR-seeded useUser/useSession hooks, and protectedRoute.

原始使用文档

@fluixi/auth

Server-first auth for Fluixi: native bindings over Better Auth. Better Auth owns the security primitives (password hashing, sessions, CSRF, secure cookies, OAuth, 2FA/TOTP, organizations, RBAC, DB adapters: audited and maintained). This package owns the Fluixi integration Better Auth can't provide: request-context server helpers, SSR-seeded reactive hooks, middleware wiring, and route protection.

better-auth is a peer dependency: the bindings consume it structurally, so the app owns the version.

Server (@fluixi/auth/server)

// auth.config.ts
import { betterAuth } from 'better-auth';
import { defineAuth } from '@fluixi/auth/server';
export const auth = defineAuth(betterAuth({ /* db, providers, plugins */ }));
// src/middleware.ts — resolve the session once per request → locals.auth
import { defineMiddleware } from '@fluixi/start';
import { authMiddleware } from '@fluixi/auth/server';
export default defineMiddleware([ authMiddleware() ]);
// src/api/auth/[...all].ts — mount Better Auth
import { mountAuthRoutes } from '@fluixi/auth/server';
export const ALL = mountAuthRoutes(); // (request) => auth.handler(request)
// loaders / server functions — no token parsing, reads request-scoped locals
const user  = await requireUser();            // throws AuthError(401, '/login')
const admin = await requireRole('admin');     // throws AuthError(403)
await requirePermission(() => can(user, 'edit', post));
const maybe = await optionalUser();           // U | null
// API route contracts (@fluixi/api) take a guard instead: it runs before the input is read
// and types `auth` in the handler.
import { route } from '@fluixi/api';
import { member, signedIn } from '@fluixi/auth/server';

export const GET = route({ guard: signedIn(), handler: ({ auth }) => auth.user });
export const DELETE = route({
  guard: member({ roles: ['owner', 'admin'] }), // 403 no_active_organization, not_a_member, forbidden
  handler: ({ auth }) => projects.remove(auth.member.organizationId),
});

The app entry must forward the request so middleware locals reach the render: export const render = (url, request) => renderRequestAsync(App, { url, request }).

Client (@fluixi/auth/client)

import { createAuthClient } from 'better-auth/client';
import { createAuthHooks } from '@fluixi/auth/client';

const authClient = createAuthClient({ baseURL: '/api/auth' });
export const { useUser, useSession, useAuth, useSignIn, useSignOut, protectedRoute } =
  createAuthHooks({ client: authClient });
const user = useUser();                 // reactive accessor, SEEDED from SSR (no flash/refetch)
const signIn = useSignIn();
export default protectedRoute(() => <Dashboard/>);          // authed only
export default protectedRoute(Login, { guestOnly: true });  // redirect authed away
export default protectedRoute(Admin, { role: 'admin' });    // role-gated

The session is a createIsomorphicStore: resolved on the server from locals.auth, serialized into the page, and seeded synchronously on the client. No auth flash, no post-hydration refetch. @fluixi/auth/client never imports the server engine or Node.

版本与发布记录

@fluixi/auth 共发布 56 个版本,首次发布于 2026/07/10;过去 12 个月发布了 56 个版本。

版本发布日期距今
1.0.0-alpha.108latest2026/11/078 天前
1.0.0-alpha.1072026/11/0511 天前
1.0.0-alpha.1062026/11/0213 天前
1.0.0-alpha.1052026/11/0115 天前
1.0.0-alpha.1042026/11/0115 天前
1.0.0-alpha.1032026/10/2620 天前

关于 @fluixi/auth 的常见问题

@fluixi/auth 是什么?

Server-first auth for Fluixi — native bindings over Better Auth: authMiddleware, requireUser/Role/Permission (request-context, no token parsing), SSR-seeded useUser/useSession hooks, and protectedRoute.

如何安装 @fluixi/auth?

在项目目录运行 npm install @fluixi/auth,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@fluixi/dom(^1.0.0-alpha.108)、@fluixi/core(^1.0.0-alpha.108)、@fluixi/reactive(^1.0.0-alpha.108)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fluixi/auth 支持 TypeScript 吗?

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

@fluixi/auth 支持 ESM 还是 CommonJS?

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

@fluixi/auth 可以商用吗?

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

@fluixi/auth 的下载量有多少?

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

@fluixi/auth 还在维护吗?

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