session

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

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 中

func Get

func Get(ctx *gctx.Context) (Session, error)

Get 使用默认 Provider 获取 Session

func NewSession

func NewSession(ctx *gctx.Context, userId string, jwtData map[string]string, sessData map[string]any) (Session, error)

NewSession 使用默认 Provider 创建 Session

type TokenCarrier

type TokenCarrier interface {
	// Inject 将 Token 注入到响应中
	Inject(ctx *gctx.Context, token string)

	// Extract 从请求中提取 Token
	Extract(ctx *gctx.Context) string

	// Clear 清除 Token
	Clear(ctx *gctx.Context)
}

TokenCarrier Token 载体接口 定义了如何在请求中携带和提取 Token

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL