mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-18 01:06:39 +08:00
fix: fixed bug for jwt token expire caculation
This commit is contained in:
parent
f4c6ca4554
commit
036a6e3e41
@ -44,7 +44,7 @@ func (h *ManagerHandler) Login(c *gin.Context) {
|
|||||||
// 创建 token
|
// 创建 token
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"user_id": manager.Username,
|
"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))
|
tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -52,7 +52,8 @@ func (h *ManagerHandler) Login(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 保存到 redis
|
// 保存到 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())
|
resp.ERROR(c, "error with save token: "+err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -64,8 +65,8 @@ func (h *ManagerHandler) Login(c *gin.Context) {
|
|||||||
|
|
||||||
// Logout 注销
|
// Logout 注销
|
||||||
func (h *ManagerHandler) Logout(c *gin.Context) {
|
func (h *ManagerHandler) Logout(c *gin.Context) {
|
||||||
token := c.GetHeader(types.AdminAuthHeader)
|
key := h.GetUserKey(c)
|
||||||
if _, err := h.redis.Del(c, token).Result(); err != nil {
|
if _, err := h.redis.Del(c, key).Result(); err != nil {
|
||||||
logger.Error("error with delete session: ", err)
|
logger.Error("error with delete session: ", err)
|
||||||
} else {
|
} else {
|
||||||
resp.SUCCESS(c)
|
resp.SUCCESS(c)
|
||||||
|
@ -2,8 +2,10 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"chatplus/core"
|
"chatplus/core"
|
||||||
|
"chatplus/core/types"
|
||||||
logger2 "chatplus/logger"
|
logger2 "chatplus/logger"
|
||||||
"chatplus/utils"
|
"chatplus/utils"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"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 {
|
func (h *BaseHandler) PostBool(c *gin.Context, key string) bool {
|
||||||
return utils.BoolValue(c.PostForm(key))
|
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
|
// 创建 token
|
||||||
expired := time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge))
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||||
"user_id": user.Id,
|
"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))
|
tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -182,8 +181,8 @@ func (h *UserHandler) Login(c *gin.Context) {
|
|||||||
// Logout 注 销
|
// Logout 注 销
|
||||||
func (h *UserHandler) Logout(c *gin.Context) {
|
func (h *UserHandler) Logout(c *gin.Context) {
|
||||||
sessionId := c.GetHeader(types.ChatTokenHeader)
|
sessionId := c.GetHeader(types.ChatTokenHeader)
|
||||||
token := c.GetHeader(types.UserAuthHeader)
|
key := h.GetUserKey(c)
|
||||||
if _, err := h.redis.Del(c, token).Result(); err != nil {
|
if _, err := h.redis.Del(c, key).Result(); err != nil {
|
||||||
logger.Error("error with delete session: ", err)
|
logger.Error("error with delete session: ", err)
|
||||||
}
|
}
|
||||||
// 删除 websocket 会话列表
|
// 删除 websocket 会话列表
|
||||||
|
Loading…
Reference in New Issue
Block a user