feat: add email verify code endpoint and require code on register
- POST /v1/user/email/code sends a 6-digit verify code via email (rate-limited, 10min TTL) - RegisterByEmail now validates verify_code before creating the account - Added email code cache helpers mirroring SMS pattern - Added error codes 1007 (email code error) and 1008 (send limit) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -106,6 +106,26 @@ func (h *UserEndpoint) GetUserInfo(ctx *gin.Context) {
|
||||
}))
|
||||
}
|
||||
|
||||
func (h *UserEndpoint) SendEmailVerifyCode(ctx *gin.Context) {
|
||||
req := model.EmailVerifyCodeRequest{}
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeParamError, common.CodeParamErrorMsg))
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.userService.SendEmailVerifyCode(ctx, req.Email); err != nil {
|
||||
if bizErr, ok := err.(*common.BusinessError); ok {
|
||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
||||
return
|
||||
}
|
||||
log.Error(ctx, "func", "SendEmailVerifyCode", "msg", "发送邮件验证码失败", "error", err)
|
||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, common.CodeSystemError, common.CodeSystemErrorMsg))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, common.SuccessResponse(ctx, model.EmailVerifyCodeResponse{}))
|
||||
}
|
||||
|
||||
func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
||||
req := model.EmailRegisterRequest{}
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
@@ -113,7 +133,7 @@ func (h *UserEndpoint) RegisterByEmail(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password)
|
||||
uid, err := h.userService.RegisterByEmail(ctx, req.Email, req.Password, req.VerifyCode)
|
||||
if err != nil {
|
||||
if bizErr, ok := err.(*common.BusinessError); ok {
|
||||
ctx.JSON(http.StatusOK, common.ErrorResponse(ctx, int(bizErr.Code), bizErr.Message))
|
||||
|
||||
Reference in New Issue
Block a user