package filter import ( "ai-gateway/common/constant" "ai-gateway/infrastructure/repository" "ai-gateway/service" "context" "net/http" "github.com/labstack/echo/v4" ) var unauthorizedResp = map[string]interface{}{ "code": http.StatusUnauthorized, "msg": "Login failed, please try again.", } // UserAuth 新版本 func UserAuth(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { fbc := context.WithValue(context.Background(), constant.DBKey, repository.Db) loginResult := service.Login.GetLoginResult(fbc, &c) if loginResult == nil { return c.JSON(http.StatusOK, unauthorizedResp) } //存入数据 c.Set(constant.UID, loginResult.Uid) return next(c) } }