:refactor: 移除不必要的getProtocol函数,简化回调URL构建逻辑
All checks were successful
BuildImage / build-image (push) Successful in 3m3s
All checks were successful
BuildImage / build-image (push) Successful in 3m3s
This commit is contained in:
parent
06c1cd91f3
commit
c320e8951f
@ -2,7 +2,6 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitee.ltd/lxh/wechat-robot/internal/types"
|
"gitee.ltd/lxh/wechat-robot/internal/types"
|
||||||
@ -16,29 +15,10 @@ import (
|
|||||||
|
|
||||||
// 实现Logto会话存储接口
|
// 实现Logto会话存储接口
|
||||||
var store = session.New(session.Config{
|
var store = session.New(session.Config{
|
||||||
KeyLookup: "cookie:logto-session",
|
KeyLookup: "cookie:logto-session",
|
||||||
CookieSecure: false, // 开发环境可设成false
|
CookieSecure: false, // 开发环境可设成false
|
||||||
CookieHTTPOnly: true,
|
|
||||||
// 可以配置 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登录页面
|
// LogtoLogin 重定向到Logto登录页面
|
||||||
func LogtoLogin(c *fiber.Ctx) error {
|
func LogtoLogin(c *fiber.Ctx) error {
|
||||||
// 加载配置
|
// 加载配置
|
||||||
@ -48,8 +28,8 @@ func LogtoLogin(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 构建回调URL
|
// 构建回调URL
|
||||||
|
callbackURL := c.Protocol() + "://" + c.Hostname() + "/auth/logto/callback"
|
||||||
callbackURL := getProtocol(c) + "://" + c.Hostname() + "/auth/logto/callback"
|
//callbackURL := "https://wechat.0bug.dev/auth/logto/callback"
|
||||||
|
|
||||||
// 创建Logto客户端配置
|
// 创建Logto客户端配置
|
||||||
logtoConfig := &logtoClient.LogtoConfig{
|
logtoConfig := &logtoClient.LogtoConfig{
|
||||||
@ -62,14 +42,8 @@ func LogtoLogin(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// 创建Logto客户端
|
// 创建Logto客户端
|
||||||
client := logtoClient.NewLogtoClient(logtoConfig, fiberSessionStorage)
|
client := logtoClient.NewLogtoClient(logtoConfig, fiberSessionStorage)
|
||||||
|
// 获取登录链接
|
||||||
// 创建登录选项,确保包含正确的回调URL
|
signInUri, err := client.SignInWithRedirectUri(callbackURL)
|
||||||
signInOptions := &logtoClient.SignInOptions{
|
|
||||||
RedirectUri: callbackURL,
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取登录链接并重定向
|
|
||||||
signInUri, err := client.SignIn(signInOptions)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Redirect("/error?error=Logto登录错误: " + err.Error() + "。请在Logto控制台确认回调URI为: " + callbackURL)
|
return c.Redirect("/error?error=Logto登录错误: " + err.Error() + "。请在Logto控制台确认回调URI为: " + callbackURL)
|
||||||
}
|
}
|
||||||
@ -102,8 +76,7 @@ func LogtoCallback(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 处理回调
|
// 处理回调
|
||||||
err = client.HandleSignInCallback(r)
|
if err = client.HandleSignInCallback(r); err != nil {
|
||||||
if err != nil {
|
|
||||||
return c.Redirect("/error?error=Logto回调处理错误: " + err.Error())
|
return c.Redirect("/error?error=Logto回调处理错误: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +113,7 @@ func LogtoLogout(c *fiber.Ctx) error {
|
|||||||
client := logtoClient.NewLogtoClient(logtoConfig, fiberSessionStorage)
|
client := logtoClient.NewLogtoClient(logtoConfig, fiberSessionStorage)
|
||||||
|
|
||||||
// 构建登出后重定向URL
|
// 构建登出后重定向URL
|
||||||
postLogoutRedirectURL := getProtocol(c) + "://" + c.Hostname()
|
postLogoutRedirectURL := c.Protocol() + "://" + c.Hostname()
|
||||||
|
|
||||||
// 获取登出链接
|
// 获取登出链接
|
||||||
signOutUri, err := client.SignOut(postLogoutRedirectURL)
|
signOutUri, err := client.SignOut(postLogoutRedirectURL)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user