package users import ( "astra-api-gateway/config" "astra-api-gateway/model" "astra-api-gateway/pkg/api" "encoding/json" "github.com/golang-jwt/jwt/v5" "github.com/labstack/echo/v4" "net/http" "strconv" "time" ) func Login(c echo.Context) (err error) { u := new(model.User) data, err, _ := api.PostRequest(config.Env.MSHosts.UsersHost+"/login", c.Request().Body, c.Request().Header) if err != nil { return c.NoContent(http.StatusBadGateway) } err = json.Unmarshal(data, &u) if err != nil { return c.NoContent(http.StatusUnauthorized) } token := jwt.New(jwt.SigningMethodHS256) claims := token.Claims.(jwt.MapClaims) claims["id"] = u.ID claims["exp"] = time.Now().Add(time.Hour * 72).Unix() claims["role"] = u.Role claims["position_name"] = u.Position.Name claims["position_rm"] = strconv.Itoa(u.Position.RightMask) claims["position_org_id"] = u.Position.OrganizationID var _token, _ = token.SignedString([]byte(config.Env.Server.JWTSecret)) if err != nil { return } c.SetCookie(&http.Cookie{ Name: "accessToken", Value: _token, Path: "/", Expires: time.Now().Add(24 * time.Hour), Secure: true, SameSite: 4, }) return c.JSON(http.StatusOK, u) } func Logout(c echo.Context) error { c.SetCookie(&http.Cookie{ Name: "accessToken", Value: "", Path: "/", Expires: time.Unix(0, 0), Secure: true, SameSite: 4, }) return c.NoContent(http.StatusOK) } func GetCurrentUser(c echo.Context) error { data, err, _ := api.GetRequest(config.Env.MSHosts.UsersHost+"/me", c.Request().Header) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.JSONBlob(http.StatusOK, data) } func GetUsers(c echo.Context) error { id := c.Param("id") data, err, _ := api.GetRequest( config.Env.MSHosts.UsersHost+"/"+id, c.Request().Header, c.Request().URL.RawQuery) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.JSONBlob(http.StatusOK, data) } func DeleteUser(c echo.Context) error { id := c.Param("id") data, err, resp := api.DeleteRequest( config.Env.MSHosts.UsersHost+"/"+id, c.Request().Header) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data) } func UpdateUser(c echo.Context) (err error) { id := c.Param("id") // Bind u := new(model.User) if err = c.Bind(u); err != nil { return } data, err := api.PatchRequest(config.Env.MSHosts.UsersHost+"/"+id, u, c.Request().Header) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.JSONBlob(http.StatusOK, data) } func GetOrganizations(c echo.Context) error { data, err, resp := api.GetRequest( config.Env.MSHosts.UsersHost+"/organizations", c.Request().Header, c.Request().URL.RawQuery) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data) } func GetPositions(c echo.Context) error { data, err, resp := api.GetRequest( config.Env.MSHosts.UsersHost+"/positions", c.Request().Header, c.Request().URL.RawQuery) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data) } func UploadAvatar(c echo.Context) error { id := c.Param("id") data, err, resp := api.PostRequest( config.Env.MSHosts.UsersHost+"/"+id+"/avatar", c.Request().Body, c.Request().Header) if err != nil { return c.NoContent(http.StatusBadGateway) } return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data) }