48 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}