Documentation
¶
Index ¶
Constants ¶
View Source
const (
// CtxSessionKey 在 Context 中存储 Session 的 key
CtxSessionKey = "gint:session"
)
Variables ¶
This section is empty.
Functions ¶
func SetDefaultProvider ¶
func SetDefaultProvider(provider Provider)
SetDefaultProvider 设置默认的 Session Provider 注意:应该在程序启动时调用一次,不要在运行时频繁调用
Types ¶
type Provider ¶
type Provider interface {
// NewSession 创建新会话
// userId: 用户 ID(string 类型)
// jwtData: 存储在 JWT 中的数据
// sessData: 存储在 Session 中的数据
NewSession(ctx *gctx.Context, userId string, jwtData map[string]string, sessData map[string]any) (Session, error)
// Get 获取会话
// 会自动验证会话的有效性
Get(ctx *gctx.Context) (Session, error)
// Destroy 销毁会话
Destroy(ctx *gctx.Context) error
// RenewToken 刷新 Token
// 在 Token 即将过期时调用
RenewToken(ctx *gctx.Context) error
}
Provider 会话提供者接口 负责创建、获取、销毁会话
type Session ¶
type Session interface {
// Set 设置会话数据
Set(ctx context.Context, key string, val any) error
// Get 获取会话数据
Get(ctx context.Context, key string) (any, error)
// Del 删除会话数据
Del(ctx context.Context, key string) error
// Destroy 销毁整个会话
Destroy(ctx context.Context) error
// Claims 获取 JWT 中的声明数据
Claims() *jwt.Claims
// Refresh 刷新会话过期时间
Refresh(ctx context.Context) error
}
Session 会话接口 混合了 JWT 的设计,轻量数据存储在 JWT 中,完整数据存储在 Redis 中
Click to show internal directories.
Click to hide internal directories.