feat(auth): 邮箱验证码注册,生产重新有了自助入口
CI / backend (push) Canceled after 0s

短信登录关掉后,生产没有任何注册入口——这一版补上最普通的那条路:
填邮箱、收验证码、设密码,注册完直接进首页。

先发码再注册,不是"注册时顺带发信"

  POST /auth/email/code 只做三件事:查重、限流、发码。
  填完密码才被告知"这个邮箱注册过了"最气人,所以占用(2019)在发码前就回。

邮箱既是账号名也是唯一联系方式,所以必须真验一次码

  验证通过时间落 app_user.email_verified_at,/auth/me 回 email_verified。
  "随手填一个"会让账号变成没人能找回的孤岛。

app_user.phone 改为可空(迁移 0011)

  邮箱注册的账号本来就没有手机号。user.phone 对这类账号回空串,
  成员列表同理,不再伪装成一串星号。

防刷

  同邮箱 60 秒一次(2022)、验证码 10 分钟有效、错 5 次即作废(2021)。
  6 位数可以被刮,不设上限等于把注册口子交给脚本。

本机联调开关 EMAIL_CODE_DEV_ECHO

  为 true 时把码回给调用方且不发真信——否则每跑一次 e2e 就往真实收件箱塞一封。
  生产打开会让 validate_runtime() 拒绝启动。

前端

  新增 /register(邮箱、验证码、密码、确认、昵称;60 秒倒计时;
  服务端关着注册时整页只说"联系管理员开通"),登录页按 /auth/methods
  决定要不要显示「还没有账号?用邮箱注册」。

验证

  pytest 139 passed、ruff check / format 全绿
  tsc --noEmit 0、vite build 通过
  e2e register.mjs 7 项:登录页入口 → 拿码 → 注册落进首页 → 退出用邮箱+密码登回来
    → 同一邮箱再发码回 2019 → 控制台零报错
  e2e login-email-smtp.mjs 15 项回归通过(含真发一封测试信)
  e2e account.mjs 14 项、knowledge.mjs 全绿

顺带修的两处测试

  test_account 的字段白名单补 email_verified(接口确实多了这个字段);
  test_config_prod 用 Settings(_env_file=None, ...) 构造,别被本机 .env 里的
  EMAIL_CODE_DEV_ECHO=true 带偏——那个开关在生产是硬错误,本机却是跑 e2e 必需的。
This commit is contained in:
Steven
2026-11-03 01:04:26 +08:00
parent c0c5230fd1
commit b5170ad1ce
20 changed files with 955 additions and 16 deletions
+144 -4
View File
@@ -1,9 +1,12 @@
"""认证:手机号 / 邮箱 + 密码,手机验证码可选。
"""认证:手机号 / 邮箱 + 密码,邮箱验证码注册,手机验证码可选。
当前**默认关掉短信验证码**(`SMS_LOGIN_ENABLED=false`):内测阶段的固定验证码
等于"知道手机号就能进任意账号"。关掉后 `/auth/sms/send` 与 `/auth/login` 直接
回 2017,登录走「手机号 或 邮箱 + 密码」。登录页先问 `/auth/methods`,只显示
服务端真开着的登录方式,免得点进去才报错。
新用户走「邮箱 + 验证码」注册(`POST /auth/email/code` → `POST /auth/register`):
短信一关,就没有别的注册入口了。注册出来的账号没有手机号,`app_user.phone` 因此可空。
"""
from __future__ import annotations
@@ -11,10 +14,12 @@ from __future__ import annotations
import hmac
import re
import secrets
from datetime import datetime, timezone
from fastapi import APIRouter
from pydantic import BaseModel, Field, model_validator
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from ..config import get_settings
from ..deps import CurrentUser, SessionDep
@@ -28,7 +33,15 @@ from ..security import (
password_problem,
verify_password,
)
from ..stores import get_store, sms_code_key, sms_throttle_key
from ..services import mail
from ..stores import (
email_code_key,
email_throttle_key,
email_tries_key,
get_store,
sms_code_key,
sms_throttle_key,
)
router = APIRouter(prefix="/auth", tags=["认证"])
settings = get_settings()
@@ -42,7 +55,9 @@ PASSWORD_FAIL_LIMIT = 10
PASSWORD_FAIL_TTL = 900
def mask_phone(phone: str) -> str:
def mask_phone(phone: str | None) -> str:
if not phone:
return ""
return f"{phone[:3]}****{phone[-4:]}" if len(phone) == 11 else "***"
@@ -60,10 +75,11 @@ def _user_out(user: AppUser) -> dict:
"id": str(user.id),
"phone": mask_phone(user.phone),
"email": user.email,
"email_verified": user.email_verified_at is not None,
"nickname": user.nickname,
"avatar_url": user.avatar_url,
"has_password": bool(user.password_hash),
"platform_admin": user.phone in settings.platform_admin_phones,
"platform_admin": bool(user.phone) and user.phone in settings.platform_admin_phones,
"created_at": user.created_at,
}
@@ -125,6 +141,7 @@ async def methods() -> dict:
"password": True,
"email_password": True,
"sms_code": settings.sms_login_enabled,
"email_register": settings.email_register_enabled,
}
)
@@ -321,3 +338,126 @@ async def set_password(body: PasswordIn, user: CurrentUser, session: SessionDep)
row.password_hash = hash_password(body.password)
await session.flush()
return ok({"has_password": True})
# ── 邮箱注册 ────────────────────────────────────────────
class EmailCodeIn(BaseModel):
email: str = Field(max_length=255, examples=["someone@example.com"])
class RegisterIn(BaseModel):
"""邮箱 + 验证码 + 密码,一步把账号建出来。
昵称不填就取邮箱 @ 前面那段,别让新用户第一眼看到一个空名字。
"""
email: str = Field(max_length=255)
code: str = Field(min_length=4, max_length=8)
password: str = Field(min_length=1, max_length=64)
nickname: str | None = Field(default=None, max_length=32)
async def _email_taken(session: AsyncSession, email: str) -> bool:
stmt = select(AppUser.id).where(func.lower(AppUser.email) == email)
return (await session.execute(stmt)).first() is not None
@router.post("/email/code", summary="发送邮箱注册验证码")
async def send_email_code(body: EmailCodeIn, session: SessionDep) -> dict:
"""发码前先查邮箱是否已被占用:让用户填完密码才发现"这个邮箱注册过了"最气人。"""
if not settings.email_register_enabled:
raise AppError(
ErrorCode.LOGIN_METHOD_DISABLED,
"邮箱注册暂未开放,请联系管理员开通账号",
)
email = normalize_email(body.email)
if not EMAIL_RE.match(email):
raise AppError(ErrorCode.EMAIL_INVALID, "邮箱格式不对")
if await _email_taken(session, email):
raise AppError(ErrorCode.EMAIL_TAKEN, "这个邮箱已经注册过了,直接登录即可")
store = get_store()
if settings.email_code_throttle_seconds > 0:
hits = await store.incr(email_throttle_key(email), settings.email_code_throttle_seconds)
if hits > 1:
raise AppError(
ErrorCode.EMAIL_CODE_TOO_FREQUENT,
f"发送过于频繁,请 {settings.email_code_throttle_seconds} 秒后重试",
)
code = f"{secrets.randbelow(1_000_000):06d}"
await store.set(email_code_key(email), code, settings.email_code_ttl_seconds)
await store.delete(email_tries_key(email))
minutes = max(1, settings.email_code_ttl_seconds // 60)
if settings.email_code_dev_echo:
# 本机联调:不发真信,直接把码回给调用方(生产打开会被 validate_runtime 拒掉)
return ok(
{
"sent": True,
"expires_in": settings.email_code_ttl_seconds,
"dev_code": code,
}
)
await mail.send_verify_code(session, to=email, code=code, minutes=minutes)
return ok({"sent": True, "expires_in": settings.email_code_ttl_seconds})
@router.post("/register", summary="邮箱注册(验证码 + 密码)")
async def register(body: RegisterIn, session: SessionDep) -> dict:
"""邮箱注册:验码 → 建账号 → 直接发令牌,注册完就是登录状态。
账号没有手机号(`phone` 为空),所以邮件是唯一联系方式——这也是为什么注册必须
真验一次码,而不是"填个邮箱就算"。
"""
if not settings.email_register_enabled:
raise AppError(
ErrorCode.LOGIN_METHOD_DISABLED,
"邮箱注册暂未开放,请联系管理员开通账号",
)
email = normalize_email(body.email)
if not EMAIL_RE.match(email):
raise AppError(ErrorCode.EMAIL_INVALID, "邮箱格式不对")
store = get_store()
stored = await store.get(email_code_key(email))
if not stored:
raise AppError(ErrorCode.EMAIL_CODE_INVALID, "验证码已过期,请重新获取")
tries = await store.incr(email_tries_key(email), settings.email_code_ttl_seconds)
if tries > settings.email_code_max_attempts:
await store.delete(email_code_key(email))
raise AppError(ErrorCode.EMAIL_CODE_INVALID, "验证码错太多次,请重新获取")
if not hmac.compare_digest(stored, body.code.strip()):
raise AppError(ErrorCode.EMAIL_CODE_INVALID, "验证码不对")
problem = password_problem(body.password)
if problem:
raise AppError(ErrorCode.PASSWORD_WEAK, problem)
# 拿到验证码之后这段时间里,别人可能已经用同一个邮箱注册完了
if await _email_taken(session, email):
raise AppError(ErrorCode.EMAIL_TAKEN, "这个邮箱已经注册过了,直接登录即可")
nickname = (body.nickname or "").strip() or email.split("@", 1)[0][:32]
user = AppUser(
email=email,
phone=None,
nickname=nickname,
password_hash=hash_password(body.password),
email_verified_at=datetime.now(timezone.utc),
status="active",
)
session.add(user)
await session.flush()
await store.delete(email_code_key(email))
await store.delete(email_tries_key(email))
await store.delete(email_throttle_key(email))
tokens = create_token_pair(user.id)
return ok({"user": _user_out(user), "tokens": _tokens_out(tokens)})