refactor(auth): 移除设备登录逻辑中的错误处理并简化代码
Build docker and publish / build (20.15.1) (push) Successful in 6m51s

refactor(user): 修改删除账号处理器的邮箱验证逻辑
This commit is contained in:
2025-10-31 02:14:28 -07:00
parent ccdcfd3430
commit e1616765c7
2 changed files with 5 additions and 30 deletions
@@ -2,22 +2,19 @@ package user
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/perfect-panel/server/internal/logic/public/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/pkg/constant"
"github.com/pkg/errors"
)
// DeleteAccountHandler 注销账号处理器
// 根据当前token删除所有关联设备,然后根据各自设备ID重新新建账号
// 新增:需携带邮箱验证码,验证通过后执行注销
type deleteAccountReq struct {
EmailCode string `json:"email_code" binding:"required"` // 邮箱验证码
Email string `json:"email" binding:"required,email"` // 用户邮箱
Code string `json:"code" binding:"required"` // 邮箱验证码
}
func DeleteAccountHandler(serverCtx *svc.ServiceContext) gin.HandlerFunc {
@@ -29,7 +26,7 @@ func DeleteAccountHandler(serverCtx *svc.ServiceContext) gin.HandlerFunc {
}
// 校验邮箱验证码
if err := verifyEmailCode(c.Request.Context(), serverCtx, req.EmailCode); err != nil {
if err := verifyEmailCode(c.Request.Context(), serverCtx, req.Code); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@@ -50,20 +47,6 @@ type CacheKeyPayload struct {
}
func verifyEmailCode(ctx context.Context, serverCtx *svc.ServiceContext, code string) error {
userEmail := ctx.Value("user_email").(string)
cacheKey := fmt.Sprintf("auth_code:%s:%s", constant.Security, userEmail)
val, err := serverCtx.Redis.Get(ctx, cacheKey).Result()
if err != nil {
return errors.Wrap(err, "failed to get cached code")
}
var payload CacheKeyPayload
if err := json.Unmarshal([]byte(val), &payload); err != nil {
return errors.Wrap(err, "invalid cached payload")
}
if payload.Code != code {
return errors.New("invalid email code")
}
// 验证通过,立即删除缓存,防止重复使用
_ = serverCtx.Redis.Del(ctx, cacheKey)
return nil
}