:feature: 添加获取请求协议的函数,优化Logto登录和登出URL构建
All checks were successful
BuildImage / build-image (push) Successful in 8m52s

This commit is contained in:
李寻欢 2025-04-22 17:29:22 +08:00
parent 8a02f2f175
commit 8c11c0375a

View File

@ -2,6 +2,7 @@ package handler
import (
"net/http"
"strings"
"time"
"gitee.ltd/lxh/wechat-robot/internal/types"
@ -21,6 +22,23 @@ var store = session.New(session.Config{
// 可以配置 CookieSecret
})
// getProtocol
// @description: 获取请求协议
// @param c
// @return string
func getProtocol(c *fiber.Ctx) string {
protocol := c.Protocol()
for k, v := range c.GetReqHeaders() {
if strings.ToLower(k) == "referer" {
refererProtocol := strings.Split(v[0], ":")[0]
if refererProtocol != protocol {
protocol = refererProtocol
}
}
}
return protocol
}
// LogtoLogin 重定向到Logto登录页面
func LogtoLogin(c *fiber.Ctx) error {
// 加载配置
@ -30,7 +48,8 @@ func LogtoLogin(c *fiber.Ctx) error {
}
// 构建回调URL
callbackURL := c.Protocol() + "://" + c.Hostname() + "/auth/logto/callback"
callbackURL := getProtocol(c) + "://" + c.Hostname() + "/auth/logto/callback"
// 创建Logto客户端配置
logtoConfig := &logtoClient.LogtoConfig{
@ -121,7 +140,7 @@ func LogtoLogout(c *fiber.Ctx) error {
client := logtoClient.NewLogtoClient(logtoConfig, fiberSessionStorage)
// 构建登出后重定向URL
postLogoutRedirectURL := c.Protocol() + "://" + c.Hostname()
postLogoutRedirectURL := getProtocol(c) + "://" + c.Hostname()
// 获取登出链接
signOutUri, err := client.SignOut(postLogoutRedirectURL)