mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 08:13:43 +08:00 
			
		
		
		
	fix: fixed bug for jwt token expire caculation
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 会话列表
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user