1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package session
- import (
- "encoding/gob"
- "x-ui/database/model"
- "github.com/gin-contrib/sessions"
- "github.com/gin-gonic/gin"
- )
- const loginUser = "LOGIN_USER"
- func init() {
- gob.Register(model.User{})
- }
- func SetLoginUser(c *gin.Context, user *model.User) error {
- s := sessions.Default(c)
- s.Set(loginUser, user)
- return s.Save()
- }
- func SetMaxAge(c *gin.Context, maxAge int) error {
- s := sessions.Default(c)
- s.Options(sessions.Options{
- Path: "/",
- MaxAge: maxAge,
- })
- return s.Save()
- }
- func GetLoginUser(c *gin.Context) *model.User {
- s := sessions.Default(c)
- if obj := s.Get(loginUser); obj != nil {
- if user, ok := obj.(model.User); ok {
- return &user
- }
- }
- return nil
- }
- func IsLogin(c *gin.Context) bool {
- return GetLoginUser(c) != nil
- }
- func ClearSession(c *gin.Context) error {
- s := sessions.Default(c)
- s.Clear()
- s.Options(sessions.Options{
- Path: "/",
- MaxAge: -1,
- })
- if err := s.Save(); err != nil {
- return err
- }
- c.SetCookie("3x-ui", "", -1, "/", "", false, true)
- return nil
- }
|