refactor(auth): 移除设备登录逻辑中的错误处理并简化代码
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m51s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m51s
refactor(user): 修改删除账号处理器的邮箱验证逻辑
This commit is contained in:
parent
ccdcfd3430
commit
e1616765c7
@ -2,22 +2,19 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/perfect-panel/server/internal/logic/public/user"
|
"github.com/perfect-panel/server/internal/logic/public/user"
|
||||||
"github.com/perfect-panel/server/internal/svc"
|
"github.com/perfect-panel/server/internal/svc"
|
||||||
"github.com/perfect-panel/server/pkg/constant"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteAccountHandler 注销账号处理器
|
// DeleteAccountHandler 注销账号处理器
|
||||||
// 根据当前token删除所有关联设备,然后根据各自设备ID重新新建账号
|
// 根据当前token删除所有关联设备,然后根据各自设备ID重新新建账号
|
||||||
// 新增:需携带邮箱验证码,验证通过后执行注销
|
// 新增:需携带邮箱验证码,验证通过后执行注销
|
||||||
type deleteAccountReq struct {
|
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 {
|
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()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -50,20 +47,6 @@ type CacheKeyPayload struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyEmailCode(ctx context.Context, serverCtx *svc.ServiceContext, code string) error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,7 +43,6 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
|
|||||||
var userInfo *user.User
|
var userInfo *user.User
|
||||||
// Record login status
|
// Record login status
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
||||||
if userInfo != nil && userInfo.Id != 0 {
|
if userInfo != nil && userInfo.Id != 0 {
|
||||||
loginLog := log.Login{
|
loginLog := log.Login{
|
||||||
Method: "device",
|
Method: "device",
|
||||||
@ -98,13 +97,7 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
|
|||||||
|
|
||||||
// 根据 req 中的UA 更新UA
|
// 根据 req 中的UA 更新UA
|
||||||
deviceInfo.UserAgent = req.UserAgent
|
deviceInfo.UserAgent = req.UserAgent
|
||||||
if err := l.svcCtx.UserModel.UpdateDevice(l.ctx, deviceInfo); err != nil {
|
_ = l.svcCtx.UserModel.UpdateDevice(l.ctx, deviceInfo)
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate session id
|
// Generate session id
|
||||||
sessionId := uuidx.NewUUID().String()
|
sessionId := uuidx.NewUUID().String()
|
||||||
@ -137,7 +130,6 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Store device id in redis
|
// Store device id in redis
|
||||||
|
|
||||||
deviceCacheKey := fmt.Sprintf("%v:%v", config.DeviceCacheKeyKey, req.Identifier)
|
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 {
|
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",
|
l.Errorw("set device id error",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user