48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package logto
|
||
|
||
import (
|
||
"gitee.ltd/lxh/wechat-robot/internal/config"
|
||
"gitee.ltd/lxh/wechat-robot/internal/types"
|
||
"github.com/gofiber/fiber/v2"
|
||
"github.com/gofiber/fiber/v2/middleware/session"
|
||
"github.com/gofiber/storage/redis/v3"
|
||
logtoClient "github.com/logto-io/go/v2/client"
|
||
)
|
||
|
||
var storage *redis.Storage
|
||
|
||
// 实现Logto会话存储接口
|
||
var store *session.Store
|
||
|
||
// GetLogtoClient
|
||
// @description: 获取logto客户端
|
||
func GetLogtoClient(c *fiber.Ctx) (client *logtoClient.LogtoClient, err error) {
|
||
if storage == nil {
|
||
storage = redis.New(redis.Config{
|
||
Host: config.Scd.Redis.Host,
|
||
Port: config.Scd.Redis.Port,
|
||
Password: config.Scd.Redis.Password,
|
||
Database: config.Scd.Redis.DB,
|
||
Reset: false, // false:启动时不清除数据库,true:清除(请谨慎使用)
|
||
// 如果需要,可以配置其他选项,如 TLS、PoolSize 等
|
||
})
|
||
}
|
||
|
||
if store == nil {
|
||
store = session.New(session.Config{
|
||
Storage: storage,
|
||
KeyLookup: "cookie:logto-session",
|
||
CookieSecure: false, // 开发环境可设成false
|
||
})
|
||
}
|
||
|
||
// 创建Logto客户端配置
|
||
logtoConfig := config.Scd.Auth.Logto.GetLogtoClient()
|
||
// 获取会话
|
||
fiberSessionStorage := &types.LogtoSessionStorage{Store: store, Ctx: c}
|
||
|
||
// 创建Logto客户端
|
||
client = logtoClient.NewLogtoClient(logtoConfig, fiberSessionStorage)
|
||
return
|
||
}
|