@dbx-tools/auth-gate

中文文档
0.9.206发布于 2026/11/11Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 7926

Passwordless authentication runtime built on Better Auth, email OTP, and passkeys

原始使用文档
文档目录5 节
  1. Create a direct passwordless runtime
  2. Choose direct or tunnel integration
  3. Configure storage
  4. Use the compatibility routes
  5. Understand authentication limits

@dbx-tools/auth-gate

Create a platform-neutral passwordless runtime with Better Auth, email one-time passwords, passkeys, caller-provided identity policy, and persistent storage.

For a Databricks App behind a public tunnel, use the higher-level authGate() plugin from @dbx-tools/tunnel:

import { server } from "@databricks/appkit";
import { appkit } from "@dbx-tools/appkit";
import { email } from "@dbx-tools/email";
import { authGate, tunnelInterceptor } from "@dbx-tools/tunnel";

await appkit.createApp({
  plugins: [server(), email(), authGate({ allow: ["example.com"] })],
  interceptor: tunnelInterceptor(),
});

This composition gates only requests for the configured public tunnel host. The Databricks Apps front door and local development host continue to use AppKit identity.

Create a direct passwordless runtime

Use this package directly when you own the HTTP routing, identity policy, and code delivery:

import { auth, authStorage, type AuthEmailOptions } from "@dbx-tools/auth-gate";

async function deliverCode(email: string, code: string, options: AuthEmailOptions): Promise<void> {
  const endpoint = process.env.CODE_DELIVERY_URL;
  if (!endpoint) {
    throw new Error("CODE_DELIVERY_URL is required");
  }
  const response = await fetch(endpoint, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ email, code, ...options }),
  });
  if (!response.ok) {
    throw new Error(`Code delivery failed with status ${response.status}`);
  }
}

const database = await authStorage.createAuthStorage({ storage: "sqlite" });
const secret = process.env.AUTH_SECRET;
if (!secret) {
  throw new Error("AUTH_SECRET is required");
}

const runtime = await auth.createPasswordlessAuth({
  storage: database,
  baseURL: "http://localhost:8000",
  trustedOrigins: ["https://overlay.example.com"],
  appName: "My app",
  passkeyNamePrefix: "My Company",
  secret,
  logoutRedirectPath: "/",
  sessionTtlSeconds: 2_592_000,
  codeTtlSeconds: 600,
  maxAttempts: 5,
  authorizeIdentity: (email) => email.endsWith("@example.com"),
  sendCode: (email, code, options) => deliverCode(email, code, options),
});

Mount runtime.handler(request) under runtime.basePath, which defaults to /api/auth. Use runtime.session(headers) when the application needs the verified email address. Call runtime.close() during server shutdown so the storage adapter can release its resources.

Choose direct or tunnel integration

Use AppKit execution context for traffic that enters through the Databricks Apps front door. That front door supplies platform identity and can supply an on-behalf-of token.

Use @dbx-tools/tunnel when an AppKit application also has a public Portr or FRP entrance. Its authGate() plugin:

  • Classifies tunnel traffic by the configured Host header.
  • Mounts login and compatibility routes on AppKit's server.
  • Leaves platform front-door and local traffic ungated.
  • Sends codes through the registered @dbx-tools/email plugin.
  • Removes untrusted forwarded identity and token headers.
  • Injects the verified tunnel email as request identity.

Use @dbx-tools/auth-gate directly for another HTTP stack or a route that doesn't need tunnel classification. The direct runtime doesn't mount AppKit middleware, forward identity headers, or launch a tunnel.

A passkey or email-code session proves only the configured identity. It doesn't mint a Databricks on-behalf-of token.

Configure storage

Better Auth stores users, sessions, OTPs, rate limits, and passkeys. Storage can use an AppKit Lakebase pool or local SQLite:

import { authStorage } from "@dbx-tools/auth-gate";

const sqlite = await authStorage.createAuthStorage({
  storage: "sqlite",
  sqlitePath: "./data/auth.sqlite",
});

storage: "auto" uses a supplied Lakebase pool unless you request SQLite. If SQLite isn't available in automatic mode, the runtime uses an in-memory adapter. An explicit storage: "sqlite" setting fails instead of selecting memory. In-memory users, OTPs, sessions, and passkeys disappear when the process exits.

Better Auth migrations run under a PostgreSQL advisory lock for Lakebase or a file lock for SQLite.

Use the compatibility routes

POST <basePath>/logout returns { ok, redirectTo }. GET clears the same session and redirects with status 303. The redirect defaults to / and is restricted to a same-origin path. Better Auth accepts only baseURL and trustedOrigins. An arbitrary request Origin or Referer is rejected. Inside a Databricks App, HTTPS *.databricksapps.com origins are accepted automatically for the platform front door. WebAuthn still uses the configured baseURL host for its RP ID and expected origin.

GET <basePath>/status returns the verified session status. OTP send and verification use Better Auth's native email OTP routes under the same base path.

Understand authentication limits

  • authorizeIdentity runs for normalized lowercase email addresses. An unauthorized code request doesn't reveal whether the address is allowed.
  • The runtime stores OTP values as hashes and limits code attempts and request rates through Better Auth.
  • baseURL determines secure-cookie behavior, the WebAuthn relying-party ID, and the expected passkey origin. The relying-party ID is the exact hostname, so sibling tunnel hosts use separate passkeys. Configure the external browser origin, not a private loopback address, in production.
  • passkeyNamePrefix controls the prefix before the humanized hostname in the WebAuthn display name. It defaults to appName.
  • passkeys: false disables WebAuthn routes and advertises email OTP as the only login method. Passkeys are enabled by default.
  • trustedOrigins extends the accepted browser origins. Arbitrary Origin and Referer values are rejected.
  • The runtime is an authentication boundary, not a Databricks authorization client. Downstream Databricks calls still need AppKit or another credential source.

版本与发布记录

@dbx-tools/auth-gate 共发布 170 个版本,首次发布于 2026/09/05;过去 12 个月发布了 170 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.9.206latest2026/11/11今天
0.9.2052026/11/111 天前
0.9.2032026/11/074 天前
0.9.2022026/11/065 天前
0.9.2012026/11/056 天前
0.9.2002026/11/056 天前

关于 @dbx-tools/auth-gate 的常见问题

@dbx-tools/auth-gate 是什么?

Passwordless authentication runtime built on Better Auth, email OTP, and passkeys

如何安装 @dbx-tools/auth-gate?

在项目目录运行 npm install @dbx-tools/auth-gate,或使用 pnpm add / yarn add / bun add。

@dbx-tools/auth-gate 支持 TypeScript 吗?

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

@dbx-tools/auth-gate 支持 ESM 还是 CommonJS?

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

@dbx-tools/auth-gate 可以商用吗?

@dbx-tools/auth-gate 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dbx-tools/auth-gate 的下载量有多少?

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

@dbx-tools/auth-gate 还在维护吗?

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