diff --git a/web/controller/index.go b/web/controller/index.go index f887062a..12a2d99c 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 96293daf..2c5c62a0 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 df38fceb..2b8e994e 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 c3d04ff9..dadbfda1 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) })