fix session

This commit is contained in:
mhsanaei
2024-08-06 13:44:48 +02:00
parent d8d9c64847
commit 3e3ed4ed52
4 changed files with 32 additions and 27 deletions

View File

@@ -9,7 +9,10 @@ import (
"github.com/gin-gonic/gin"
)
const loginUser = "LOGIN_USER"
const (
loginUser = "LOGIN_USER"
defaultPath = "/"
)
func init() {
gob.Register(model.User{})
@@ -17,10 +20,6 @@ func init() {
func SetLoginUser(c *gin.Context, user *model.User) error {
s := sessions.Default(c)
s.Options(sessions.Options{
Path: "/",
HttpOnly: true,
})
s.Set(loginUser, user)
return s.Save()
}
@@ -28,20 +27,24 @@ func SetLoginUser(c *gin.Context, user *model.User) error {
func SetMaxAge(c *gin.Context, maxAge int) error {
s := sessions.Default(c)
s.Options(sessions.Options{
Path: "/",
MaxAge: maxAge,
Path: defaultPath,
MaxAge: maxAge,
HttpOnly: true,
})
return s.Save()
}
func GetLoginUser(c *gin.Context) *model.User {
s := sessions.Default(c)
if obj := s.Get(loginUser); obj != nil {
if user, ok := obj.(model.User); ok {
return &user
}
obj := s.Get(loginUser)
if obj == nil {
return nil
}
return nil
user, ok := obj.(model.User)
if !ok {
return nil
}
return &user
}
func IsLogin(c *gin.Context) bool {
@@ -52,12 +55,9 @@ func ClearSession(c *gin.Context) error {
s := sessions.Default(c)
s.Clear()
s.Options(sessions.Options{
Path: "/",
MaxAge: -1,
Path: defaultPath,
MaxAge: -1,
HttpOnly: true,
})
if err := s.Save(); err != nil {
return err
}
c.SetCookie("3x-ui", "", -1, "/", "", false, true)
return nil
return s.Save()
}