Skip to content

feat(auth): 注册登录支持手机号/邮箱任选其一,解除强绑定限制#104

Open
GaiYa0 wants to merge 1 commit into
cloudzenith:mainfrom
GaiYa0:feat/auth-flexible-registration
Open

feat(auth): 注册登录支持手机号/邮箱任选其一,解除强绑定限制#104
GaiYa0 wants to merge 1 commit into
cloudzenith:mainfrom
GaiYa0:feat/auth-flexible-registration

Conversation

@GaiYa0
Copy link
Copy Markdown

@GaiYa0 GaiYa0 commented May 27, 2026

Summary

解决原有设计中手机号和邮箱必须同时提供导致注册失败的问题,使注册/登录逻辑符合主流互联网产品设计规范。

核心变更

  • Proto 层: 移除 LoginRequestbuf.validate 强制注解,RegisterRequest.passwordmin_len 统一为 8
  • API 验证层: Register/Login 添加"至少提供手机号或邮箱之一"的前置校验,以及独立的格式校验(isValidMobile / isValidEmail
  • Service 层: accountservice.Create 添加前置参数校验;Bind 方法添加绑定前唯一性检查,防止手机号/邮箱被多账户绑定
  • Adapter 层: 新增 baseadapter.Bind / Unbind 方法;实现 BindUserVoucher / UnbindUserVoucher(消除 panic("implement me")
  • JWT Claims: 新增 AccountId 字段 + NewWithAccount 构造函数 + GetAccountId 辅助方法
  • 数据库: 新增迁移脚本 sql/20260527000001_v0_3_0_auth_upgrade.sql,为 account 表添加 Generated Column(mobile_unique / email_unique)+ 条件唯一索引
  • Bug 修复: accountrepo.ClearColumnnilNOT NULL 列 → 改为空字符串 ""

改动文件

文件 说明
backend/shortVideoApiService/api/svapi/user.proto 移除 LoginRequest 强制验证注解
backend/shortVideoApiService/internal/applications/userapp/application.go Register/Login 校验 + Bind/Unbind 实现
backend/shortVideoApiService/internal/applications/userapp/validation.go 新增:手机号/邮箱格式校验函数
backend/shortVideoApiService/internal/infrastructure/adapter/baseadapter/account.go 新增 Bind/Unbind adapter
backend/shortVideoApiService/internal/infrastructure/utils/claims/claims.go JWT Claims 新增 AccountId
backend/shortVideoApiService/go.mod 添加 baseService local replace
backend/baseService/internal/domain/service/accountservice/service.go Create/Bind 前置校验
backend/baseService/internal/infrastructure/dal/models/account.gen.go 添加 Generated Column 只读字段
backend/baseService/internal/infrastructure/repositories/accountrepo/repository.go 修复 ClearColumn nil → ""
backend/baseService/go.mod go mod tidy
sql/20260527000001_v0_3_0_auth_upgrade.sql 数据库迁移脚本

Test Plan

  • CI golangci-lint 通过
  • CI go build 通过
  • CI go test 通过
  • 手动测试:仅手机号注册 → 成功
  • 手动测试:仅邮箱注册 → 成功
  • 手动测试:手机号+邮箱注册 → 成功
  • 手动测试:均不填 → 返回错误
  • 手动测试:仅手机号登录 → 成功
  • 手动测试:仅邮箱登录 → 成功
  • 执行迁移脚本后验证唯一索引生效

Made with Cursor

解决原有设计中手机号和邮箱必须同时提供导致注册失败的问题,
使注册/登录逻辑符合主流互联网产品规范(手机号或邮箱任选其一即可)。

主要改动:
- Proto: 移除 LoginRequest 的 buf.validate 强制注解,允许可选字段
- API 层: Register/Login 方法添加"至少一个身份标识"前置校验 + 格式校验
- Service 层: accountservice.Create 添加前置校验,Bind 添加绑定前唯一性检查
- Adapter 层: 新增 Bind/Unbind 方法包装 gRPC 调用,实现 BindUserVoucher/UnbindUserVoucher
- JWT: Claims 新增 AccountId 字段,登录时携带 accountId
- DB: 新增迁移脚本,account 表添加 Generated Column + 条件唯一索引
- 修复: ClearColumn 中 nil → 空字符串,避免 NOT NULL 约束报错

Co-authored-by: Cursor <cursoragent@cursor.com>
@GaiYa0
Copy link
Copy Markdown
Author

GaiYa0 commented May 27, 2026

针对的问题是issue里v0.1.0待优化的部分:用户注册相关逻辑中,在proto中定义的参数校验要求同时传手机号+邮箱,否则会报错导致注册失败。在产品逻辑上,手机号和邮箱只需要提供一个是最为常见的逻辑,所以注册、登陆等相关逻辑中,应该允许只传手机号或邮箱,然后再对手机号或邮箱进行格式校验

在user表中,手机号和邮箱应一同设置唯一索引,或是不设置唯一索引,由代码逻辑进行判断;否则无法适配只使用手机号或是邮箱注册的逻辑

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant