|
@@ -9,7 +9,10 @@ import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
)
|
|
|
|
|
|
-const loginUser = "LOGIN_USER"
|
|
|
|
|
|
+const (
|
|
|
|
+ loginUser = "LOGIN_USER"
|
|
|
|
+ defaultPath = "/"
|
|
|
|
+)
|
|
|
|
|
|
func init() {
|
|
func init() {
|
|
gob.Register(model.User{})
|
|
gob.Register(model.User{})
|
|
@@ -17,10 +20,6 @@ func init() {
|
|
|
|
|
|
func SetLoginUser(c *gin.Context, user *model.User) error {
|
|
func SetLoginUser(c *gin.Context, user *model.User) error {
|
|
s := sessions.Default(c)
|
|
s := sessions.Default(c)
|
|
- s.Options(sessions.Options{
|
|
|
|
- Path: "/",
|
|
|
|
- HttpOnly: true,
|
|
|
|
- })
|
|
|
|
s.Set(loginUser, user)
|
|
s.Set(loginUser, user)
|
|
return s.Save()
|
|
return s.Save()
|
|
}
|
|
}
|
|
@@ -28,20 +27,24 @@ func SetLoginUser(c *gin.Context, user *model.User) error {
|
|
func SetMaxAge(c *gin.Context, maxAge int) error {
|
|
func SetMaxAge(c *gin.Context, maxAge int) error {
|
|
s := sessions.Default(c)
|
|
s := sessions.Default(c)
|
|
s.Options(sessions.Options{
|
|
s.Options(sessions.Options{
|
|
- Path: "/",
|
|
|
|
- MaxAge: maxAge,
|
|
|
|
|
|
+ Path: defaultPath,
|
|
|
|
+ MaxAge: maxAge,
|
|
|
|
+ HttpOnly: true,
|
|
})
|
|
})
|
|
return s.Save()
|
|
return s.Save()
|
|
}
|
|
}
|
|
|
|
|
|
func GetLoginUser(c *gin.Context) *model.User {
|
|
func GetLoginUser(c *gin.Context) *model.User {
|
|
s := sessions.Default(c)
|
|
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 {
|
|
func IsLogin(c *gin.Context) bool {
|
|
@@ -52,12 +55,9 @@ 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: "/",
|
|
|
|
- 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()
|
|
}
|
|
}
|