:refactor: 更新用户信息获取逻辑,使用Locals存储用户ID并记录错误日志
Some checks failed
BuildImage / build-image (push) Has been cancelled

This commit is contained in:
李寻欢 2025-04-28 11:33:43 +08:00
parent 26c8ae4e7d
commit c8427c851d
2 changed files with 6 additions and 4 deletions

View File

@ -18,12 +18,10 @@ import (
// ListRobots 列出所有机器人
func ListRobots(c *fiber.Ctx) error {
log.Debugf("登录用户Id: %+v", c.Get("userId"))
db := model.GetDB()
var robots []model.Robot
if err := db.Find(&robots).Error; err != nil {
if err := db.Where("user_id = ?", c.Locals("userId")).Find(&robots).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "获取机器人列表失败")
}

View File

@ -6,6 +6,7 @@ import (
"gitee.ltd/lxh/wechat-robot/internal/logto"
"gitee.ltd/lxh/wechat-robot/internal/redis"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
// IsAuthenticated
@ -54,7 +55,10 @@ func Authenticate() fiber.Handler {
return c.Redirect("/error?error=Logto登录错误: " + err.Error())
}
if userInfo, e := client.GetIdTokenClaims(); e == nil {
c.Set("userId", userInfo.Sub)
//log.Debugf("获取用户信息成功: %+v", userInfo)
c.Locals("userId", userInfo.Sub)
} else {
log.Errorf("获取用户信息失败: %v", e)
}
}