package controller import ( "net/http" "github.com/gorilla/sessions" "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" "gitrepo.ru/neonxp/nquest/api" "gitrepo.ru/neonxp/nquest/pkg/models" "gitrepo.ru/neonxp/nquest/pkg/service" ) type User struct { UserService *service.User } func (u *User) PostUserLogin(c echo.Context) error { req := new(api.PostUserLoginJSONRequestBody) if err := c.Bind(req); err != nil { return err } user, err := u.UserService.Login( c.Request().Context(), req.Email, req.Password, ) if err != nil { return c.JSON(http.StatusBadRequest, &api.ErrorResponse{ Code: http.StatusBadRequest, Message: err.Error(), }) } if err := setUser(c, user); err != nil { if err != nil { return c.JSON(http.StatusBadRequest, &api.ErrorResponse{ Code: http.StatusBadRequest, Message: err.Error(), }) } } return c.JSON(http.StatusOK, &api.UserResponse{ Id: int(user.ID), Username: user.Username, Email: user.Email, Team: api.MapUserTeam(user.Team), }) } func (u *User) PostUserRegister(c echo.Context) error { req := new(api.PostUserRegisterJSONRequestBody) if err := c.Bind(req); err != nil { return err } user, err := u.UserService.Register( c.Request().Context(), req.Username, req.Email, req.Password, req.Password2, ) if err != nil { return c.JSON(http.StatusBadRequest, &api.ErrorResponse{ Code: http.StatusBadRequest, Message: err.Error(), }) } if err := setUser(c, user); err != nil { return c.JSON(http.StatusBadRequest, &api.ErrorResponse{ Code: http.StatusBadRequest, Message: err.Error(), }) } return c.JSON(http.StatusOK, &api.UserResponse{ Id: int(user.ID), Username: user.Username, Email: user.Email, Team: api.MapUserTeam(user.Team), }) } func (u *User) PostUserLogout(c echo.Context) error { if err := setUser(c, nil); err != nil { return err } return c.NoContent(http.StatusOK) } func (u *User) GetUser(c echo.Context) error { user := u.UserService.GetUser(c) if user == nil { return c.JSON(http.StatusNotFound, &api.ErrorResponse{ Code: http.StatusNotFound, Message: "User not found", }) } return c.JSON(http.StatusOK, &api.UserResponse{ Id: int(user.ID), Username: user.Username, Email: user.Email, Team: api.MapUserTeam(user.Team), }) } func setUser(c echo.Context, user *models.User) error { sess, err := session.Get("session", c) if err != nil { return err } if user == nil { sess.Options = &sessions.Options{ Path: "/", MaxAge: -86400 * 7, HttpOnly: true, } if err := sess.Save(c.Request(), c.Response()); err != nil { return err } return nil } sess.Options = &sessions.Options{ Path: "/", MaxAge: 86400 * 7, HttpOnly: true, } sess.Values["userID"] = user.ID if err := sess.Save(c.Request(), c.Response()); err != nil { return err } return nil }