docs: add comments for all functions
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package session provides session management utilities for the 3x-ui web panel.
|
||||
// It handles user authentication state, login sessions, and session storage using Gin sessions.
|
||||
package session
|
||||
|
||||
import (
|
||||
@@ -19,6 +21,8 @@ func init() {
|
||||
gob.Register(model.User{})
|
||||
}
|
||||
|
||||
// SetLoginUser stores the authenticated user in the session.
|
||||
// The user object is serialized and stored for subsequent requests.
|
||||
func SetLoginUser(c *gin.Context, user *model.User) {
|
||||
if user == nil {
|
||||
return
|
||||
@@ -27,6 +31,8 @@ func SetLoginUser(c *gin.Context, user *model.User) {
|
||||
s.Set(loginUserKey, *user)
|
||||
}
|
||||
|
||||
// SetMaxAge configures the session cookie maximum age in seconds.
|
||||
// This controls how long the session remains valid before requiring re-authentication.
|
||||
func SetMaxAge(c *gin.Context, maxAge int) {
|
||||
s := sessions.Default(c)
|
||||
s.Options(sessions.Options{
|
||||
@@ -37,6 +43,8 @@ func SetMaxAge(c *gin.Context, maxAge int) {
|
||||
})
|
||||
}
|
||||
|
||||
// GetLoginUser retrieves the authenticated user from the session.
|
||||
// Returns nil if no user is logged in or if the session data is invalid.
|
||||
func GetLoginUser(c *gin.Context) *model.User {
|
||||
s := sessions.Default(c)
|
||||
obj := s.Get(loginUserKey)
|
||||
@@ -52,10 +60,14 @@ func GetLoginUser(c *gin.Context) *model.User {
|
||||
return &user
|
||||
}
|
||||
|
||||
// IsLogin checks if a user is currently authenticated in the session.
|
||||
// Returns true if a valid user session exists, false otherwise.
|
||||
func IsLogin(c *gin.Context) bool {
|
||||
return GetLoginUser(c) != nil
|
||||
}
|
||||
|
||||
// ClearSession removes all session data and invalidates the session.
|
||||
// This effectively logs out the user and clears any stored session information.
|
||||
func ClearSession(c *gin.Context) {
|
||||
s := sessions.Default(c)
|
||||
s.Clear()
|
||||
|
||||
Reference in New Issue
Block a user