@activescott/auth-provider-email

中文文档
3.2.2发布于 2026/09/20MITTypeScriptES Module构建来源已验证近 30 天下载 7042

Email magic link provider for @activescott/auth

原始使用文档
文档目录9 节
  1. Install
  2. Usage
  3. Routes registered
  4. How a sign-in email works
  5. Dev mode (no SMTP needed)
  6. Custom email transport
  7. Testing pattern: capture transport
  8. Documentation & example
  9. License

@activescott/auth-provider-email

npm version License: MIT

Email magic-link authentication provider for @activescott/auth.

Sends a sign-in email containing a single-use magic link and a numeric one-time code, both backed by one server-side challenge. Redeeming either finds-or-creates the user via your UserStore/IdentityStore and hands control back to the auth core to issue a session cookie.

Used in production by ramblefeed.com and tinkerbellbot.com.

Install

npm install @activescott/auth @activescott/auth-provider-email

Usage

import { Auth } from "@activescott/auth"
import { EmailProvider } from "@activescott/auth-provider-email"

const auth = new Auth({
  // ...session, identityStore, userStore, challengeStore...
  providers: [
    new EmailProvider({
      smtp: {
        host: process.env.SMTP_HOST!,
        port: Number(process.env.SMTP_PORT),
        user: process.env.SMTP_USER!,
        pass: process.env.SMTP_PASS!,
      },
      from: process.env.FROM_EMAIL!,
      template: { appName: "My App", subject: "Sign in to My App" },
    }),
  ],
})

Routes registered

The provider handles four URL patterns under /auth/email/* (mounted by your framework adapter):

MethodPathPurpose
POST/auth/email/initiateSend a magic link to the supplied email.
POST/auth/email/sendAlias of initiate.
GET/auth/email/verifyVerify the token from the link, log the user in.
POST/auth/email/verifyVerify a one-time code (when otp is enabled).
GET/auth/email/callbackAlias of verify.

How a sign-in email works

Each send creates one server-side challenge (via the Auth config's challengeStore) with two redemption paths, both single-use and sharing one expiry (default 15m):

  1. The magic link — /auth/email/verify?challenge=<id>&key=<256-bit secret>. Clicking it (GET) shows a minimal "Confirm sign-in" page; the confirm button POSTs the redemption. Email security scanners (Outlook SafeLinks, Mimecast, link previews) prefetch links with GET but never submit forms, so they cannot consume the single-use link.
  2. The numeric code — the user types it on your sign-in page instead of clicking the link. iOS and macOS detect the code in Mail and offer it via AutoFill when your input uses autoComplete="one-time-code". initiate sets an HttpOnly auth_challenge cookie binding code entry to the initiating browser; your code input POSTs to /auth/email/verify. Codes are attempt-capped: wrong code → INVALID_CREDENTIALS, too many tries → RATE_LIMITED, expired → EXPIRED_TOKEN.

There are no signing secrets to manage for sign-in emails — both the link key and the code are stored as salted hashes in the challenge and deleted on use.

Tuning: expiry (top-level, e.g. "15m") and otp: { length?, maxAttempts?, cookieName? }, or env vars EMAIL_EXPIRY, EMAIL_OTP_LENGTH, EMAIL_OTP_MAX_ATTEMPTS with emailConfigFromEnvironment.

initiate rejects an address whose domain has no dot (e.g. scott@willeke) before any store write or send, since it's almost always a missing TLD rather than a real destination. Set allowDotlessDomain: true for intranet or dev setups that sign in to a bare hostname like user@localhost.

Dev mode (no SMTP needed)

NodemailerTransport accepts an isDevelopment flag. When true, it buffers the email via Nodemailer's stream transport (no real SMTP connection) and prints the magic link to the server console — useful for local dev, examples, and CI:

import {
  EmailProvider,
  NodemailerTransport,
} from "@activescott/auth-provider-email"

new EmailProvider(
  {/* ...config... */},
  new NodemailerTransport(true), // dev mode: log link, don't send
)

Or pass no transport at all and the provider auto-creates new NodemailerTransport(process.env.NODE_ENV === "development").

Custom email transport

Ships with NodemailerTransport (SMTP). Implement EmailTransport to plug in Resend, SES, Postmark, or anything else:

import type {
  EmailProviderConfig,
  EmailTransport,
} from "@activescott/auth-provider-email"

class ResendTransport implements EmailTransport {
  async sendMagicLink(
    to: string,
    magicLink: string,
    config: EmailProviderConfig,
  ): Promise<boolean> {
    // ...call Resend API...
    return true
  }
}

new EmailProvider(config, new ResendTransport())

Testing pattern: capture transport

In e2e tests, wrap your EmailTransport in CaptureEmailTransport, expose what it records through a test-only readback route (gated on a test-mode env var and a shared-secret header), and drive the real confirm-page or code-entry flow — no SMTP server, no inbox polling.

import { CaptureEmailTransport } from "@activescott/auth-provider-email/testing"

// Install it only under your test-mode flag: what it holds are live sign-in
// credentials for as long as the challenge lives.
export const emailTransport =
  process.env.E2E_TEST_MODE === "true"
    ? new CaptureEmailTransport(new NodemailerTransport())
    : new NodemailerTransport()

// in your gated readback route:
const captured = emailTransport.getCapturedEmail(email) // { magicLink, code }

It lives at the /testing subpath rather than the package root so it stays out of your application's default import graph. See the runnable example linked below.

Documentation & example

Full docs and a runnable React Router framework-mode example with Playwright tests live in the monorepo:

→ https://github.com/activescott/auth**

License

MIT

版本与发布记录

@activescott/auth-provider-email 共发布 16 个版本,首次发布于 2026/03/17;过去 12 个月发布了 16 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.2.2latest2026/09/201 个月前
3.2.12026/09/191 个月前
3.2.02026/09/191 个月前
3.1.12026/09/181 个月前
3.1.02026/08/122 个月前
3.0.02026/08/122 个月前

关于 @activescott/auth-provider-email 的常见问题

@activescott/auth-provider-email 是什么?

Email magic link provider for @activescott/auth

如何安装 @activescott/auth-provider-email?

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

@activescott/auth-provider-email 支持 TypeScript 吗?

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

@activescott/auth-provider-email 支持 ESM 还是 CommonJS?

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

@activescott/auth-provider-email 可以商用吗?

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

@activescott/auth-provider-email 的下载量有多少?

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

@activescott/auth-provider-email 还在维护吗?

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