Skip to content

Commit

Permalink
change session name
Browse files Browse the repository at this point in the history
  • Loading branch information
MHSanaei committed Jul 5, 2024
1 parent 24b9e5b commit 02ae61f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion web/controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
1 change: 1 addition & 0 deletions web/html/xui/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@
this.loading(false);
if (msg.success) {
this.user = {};
window.location.replace(basePath + "logout");
}
},
async restartPanel() {
Expand Down
22 changes: 12 additions & 10 deletions web/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/gin-gonic/gin"
)

const (
loginUser = "LOGIN_USER"
)
const loginUser = "LOGIN_USER"

func init() {
gob.Register(model.User{})
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit 02ae61f

Please sign in to comment.