123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package session
- import (
- "encoding/gob"
- "x-ui/database/model"
- "github.com/gin-contrib/sessions"
- "github.com/gin-gonic/gin"
- )
- const (
- loginUser = "LOGIN_USER"
- defaultPath = "/"
- )
- 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: defaultPath,
- MaxAge: maxAge,
- HttpOnly: true,
- })
- return s.Save()
- }
- func GetLoginUser(c *gin.Context) *model.User {
- s := sessions.Default(c)
- obj := s.Get(loginUser)
- if obj == nil {
- return nil
- }
- user, ok := obj.(model.User)
- if !ok {
- return nil
- }
- return &user
- }
- 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: defaultPath,
- MaxAge: -1,
- HttpOnly: true,
- })
- return s.Save()
- }
|