change session name

This commit is contained in:
mhsanaei 2024-07-05 14:33:04 +02:00
parent 24b9e5bfa3
commit 02ae61fe6b
4 changed files with 15 additions and 12 deletions

View File

@ -503,6 +503,7 @@
this.loading(false); this.loading(false);
if (msg.success) { if (msg.success) {
this.user = {}; this.user = {};
window.location.replace(basePath + "logout");
} }
}, },
async restartPanel() { async restartPanel() {

View File

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

View File

@ -180,7 +180,7 @@ func (s *Server) initRouter() (*gin.Engine, error) {
assetsBasePath := basePath + "assets/" assetsBasePath := basePath + "assets/"
store := cookie.NewStore(secret) store := cookie.NewStore(secret)
engine.Use(sessions.Sessions("session", store)) engine.Use(sessions.Sessions("3x-ui", store))
engine.Use(func(c *gin.Context) { engine.Use(func(c *gin.Context) {
c.Set("base_path", basePath) c.Set("base_path", basePath)
}) })