From 02ae61fe6beb68ab8e5859a5eaa2b68514c0b454 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Fri, 5 Jul 2024 14:33:04 +0200 Subject: [PATCH] change session name --- web/controller/index.go | 2 +- web/html/xui/settings.html | 1 + web/session/session.go | 22 ++++++++++++---------- web/web.go | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/web/controller/index.go b/web/controller/index.go index f887062a8..12a2d99c5 100644 --- a/web/controller/index.go +++ b/web/controller/index.go @@ -94,7 +94,7 @@ func (a *IndexController) login(c *gin.Context) { func (a *IndexController) logout(c *gin.Context) { user := session.GetLoginUser(c) if user != nil { - logger.Info(user.Username, "logged out successfully") + logger.Info(user.Username, " logged out successfully") } session.ClearSession(c) c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path")) diff --git a/web/html/xui/settings.html b/web/html/xui/settings.html index 96293daf5..2c5c62a07 100644 --- a/web/html/xui/settings.html +++ b/web/html/xui/settings.html @@ -503,6 +503,7 @@ this.loading(false); if (msg.success) { this.user = {}; + window.location.replace(basePath + "logout"); } }, async restartPanel() { diff --git a/web/session/session.go b/web/session/session.go index df38fceb4..2b8e994e4 100644 --- a/web/session/session.go +++ b/web/session/session.go @@ -9,9 +9,7 @@ import ( "github.com/gin-gonic/gin" ) -const ( - loginUser = "LOGIN_USER" -) +const loginUser = "LOGIN_USER" func init() { gob.Register(model.User{}) @@ -34,24 +32,28 @@ func SetMaxAge(c *gin.Context, maxAge int) error { func GetLoginUser(c *gin.Context) *model.User { s := sessions.Default(c) - obj := s.Get(loginUser) - if obj == nil { - return nil + if obj := s.Get(loginUser); obj != nil { + if user, ok := obj.(model.User); ok { + return &user + } } - user := obj.(model.User) - return &user + return nil } func IsLogin(c *gin.Context) bool { return GetLoginUser(c) != nil } -func ClearSession(c *gin.Context) { +func ClearSession(c *gin.Context) error { s := sessions.Default(c) s.Clear() s.Options(sessions.Options{ Path: "/", MaxAge: -1, }) - s.Save() + if err := s.Save(); err != nil { + return err + } + c.SetCookie("3x-ui", "", -1, "/", "", false, true) + return nil } diff --git a/web/web.go b/web/web.go index c3d04ff9e..dadbfda19 100644 --- a/web/web.go +++ b/web/web.go @@ -180,7 +180,7 @@ func (s *Server) initRouter() (*gin.Engine, error) { assetsBasePath := basePath + "assets/" store := cookie.NewStore(secret) - engine.Use(sessions.Sessions("session", store)) + engine.Use(sessions.Sessions("3x-ui", store)) engine.Use(func(c *gin.Context) { c.Set("base_path", basePath) })