EasyStarter logoEasyStarter
认证服务

邮箱 OTP 登录

配置邮箱验证码一键登录

邮箱 OTP 登录

EasyStarter 内置了基于 Better Auth Email OTP 插件 的邮箱验证码登录功能。用户只需输入邮箱,即可收到一次性验证码完成登录,无需设置密码。

工作流程

  1. 用户在登录页输入邮箱地址
  2. 服务端通过 邮件服务 发送一次性验证码到该邮箱
  3. 用户输入收到的验证码
  4. 服务端验证通过后完成登录(如用户不存在则自动注册)

启用邮箱 OTP 登录

邮箱 OTP 登录通过 packages/app-config/src/app-config.ts 中的配置开关控制:

packages/app-config/src/app-config.ts
auth: {
  methods: {
    emailOtpEnabled: true,
  },
}

前置条件

邮箱 OTP 登录依赖邮件发送能力,请确保已完成 邮件服务 配置。

OTP 参数配置

验证码的行为参数在 packages/app-config/src/app-config.tsauth.otp.email 中统一配置:

packages/app-config/src/app-config.ts
auth: {
  otp: {
    email: {
      // 验证码位数
      otpLength: 6,
      // 验证码有效期(秒)
      expiresInSeconds: 300,
      // 单个验证码最大尝试次数
      allowedAttempts: 3,
      // 客户端重发冷却时间(秒)
      resendCooldownSeconds: 60,
    },
  },
}

速率限制

服务端对邮箱 OTP 相关接口配置了独立的速率限制,防止滥用:

apps/server/src/lib/auth.ts
rateLimit: {
  customRules: {
    "/email-otp/send-verification-otp": { window: 60, max: 3 },
    "/sign-in/email-otp": { window: 60, max: 10 },
  },
}
  • 发送验证码:每 60 秒最多 3 次
  • 验证登录:每 60 秒最多 10 次