diff --git a/api/handler/admin/admin_handler.go b/api/handler/admin/admin_handler.go index 4c122ef4..baecad81 100644 --- a/api/handler/admin/admin_handler.go +++ b/api/handler/admin/admin_handler.go @@ -44,7 +44,7 @@ func (h *ManagerHandler) Login(c *gin.Context) { // 创建 token token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "user_id": manager.Username, - "expired": time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge)), + "expired": time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge)).Unix(), }) tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey)) if err != nil { @@ -52,7 +52,8 @@ func (h *ManagerHandler) Login(c *gin.Context) { return } // 保存到 redis - if _, err := h.redis.Set(context.Background(), "users/"+manager.Username, tokenString, 0).Result(); err != nil { + key := "users/" + manager.Username + if _, err := h.redis.Set(context.Background(), key, tokenString, 0).Result(); err != nil { resp.ERROR(c, "error with save token: "+err.Error()) return } @@ -64,8 +65,8 @@ func (h *ManagerHandler) Login(c *gin.Context) { // Logout 注销 func (h *ManagerHandler) Logout(c *gin.Context) { - token := c.GetHeader(types.AdminAuthHeader) - if _, err := h.redis.Del(c, token).Result(); err != nil { + key := h.GetUserKey(c) + if _, err := h.redis.Del(c, key).Result(); err != nil { logger.Error("error with delete session: ", err) } else { resp.SUCCESS(c) diff --git a/api/handler/base_handler.go b/api/handler/base_handler.go index 8c60d0ae..6020e925 100644 --- a/api/handler/base_handler.go +++ b/api/handler/base_handler.go @@ -2,8 +2,10 @@ package handler import ( "chatplus/core" + "chatplus/core/types" logger2 "chatplus/logger" "chatplus/utils" + "fmt" "strings" "github.com/gin-gonic/gin" @@ -40,3 +42,10 @@ func (h *BaseHandler) GetBool(c *gin.Context, key string) bool { func (h *BaseHandler) PostBool(c *gin.Context, key string) bool { return utils.BoolValue(c.PostForm(key)) } +func (h *BaseHandler) GetUserKey(c *gin.Context) string { + userId, ok := c.Get(types.LoginUserID) + if !ok { + return "" + } + return fmt.Sprintf("users/%v", userId) +} diff --git a/api/handler/user_handler.go b/api/handler/user_handler.go index 1c63b072..a7597ad1 100644 --- a/api/handler/user_handler.go +++ b/api/handler/user_handler.go @@ -160,10 +160,9 @@ func (h *UserHandler) Login(c *gin.Context) { }) // 创建 token - expired := time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge)) token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "user_id": user.Id, - "expired": expired, + "expired": time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge)).Unix(), }) tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey)) if err != nil { @@ -182,8 +181,8 @@ func (h *UserHandler) Login(c *gin.Context) { // Logout 注 销 func (h *UserHandler) Logout(c *gin.Context) { sessionId := c.GetHeader(types.ChatTokenHeader) - token := c.GetHeader(types.UserAuthHeader) - if _, err := h.redis.Del(c, token).Result(); err != nil { + key := h.GetUserKey(c) + if _, err := h.redis.Del(c, key).Result(); err != nil { logger.Error("error with delete session: ", err) } // 删除 websocket 会话列表