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

refactor(user): 修改删除账号处理器的邮箱验证逻辑
This commit is contained in:
shanshanzhong 2025-10-31 02:14:28 -07:00
parent ccdcfd3430
commit e1616765c7
2 changed files with 5 additions and 30 deletions

View File

@ -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
}

View File

@ -43,7 +43,6 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
var userInfo *user.User
// Record login status
defer func() {
if userInfo != nil && userInfo.Id != 0 {
loginLog := log.Login{
Method: "device",
@ -98,13 +97,7 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
// 根据 req 中的UA 更新UA
deviceInfo.UserAgent = req.UserAgent
if err := l.svcCtx.UserModel.UpdateDevice(l.ctx, deviceInfo); err != nil {
l.Errorw("update user agent failed",
logger.Field("device_id", deviceInfo.Id),
logger.Field("error", err.Error()),
)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update user agent failed: %v", err.Error())
}
_ = l.svcCtx.UserModel.UpdateDevice(l.ctx, deviceInfo)
// Generate session id
sessionId := uuidx.NewUUID().String()
@ -137,7 +130,6 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
}
// Store device id in redis
deviceCacheKey := fmt.Sprintf("%v:%v", config.DeviceCacheKeyKey, req.Identifier)
if err = l.svcCtx.Redis.Set(l.ctx, deviceCacheKey, sessionId, time.Duration(l.svcCtx.Config.JwtAuth.AccessExpire)*time.Second).Err(); err != nil {
l.Errorw("set device id error",