2024-05-12 09:22:14 -07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-05-12 09:35:13 -07:00
|
|
|
"fmt"
|
2024-05-12 09:22:14 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/users"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *Handler) UserLogin(c echo.Context) error {
|
|
|
|
return Render(c, http.StatusOK, users.Login())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) UserAfterLogin(c echo.Context) error {
|
|
|
|
user := c.FormValue("username")
|
|
|
|
password := c.FormValue("password")
|
|
|
|
|
|
|
|
resp, err := h.api.Users.Login(user, password)
|
|
|
|
if err != nil {
|
|
|
|
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
|
|
|
|
}
|
|
|
|
|
|
|
|
cookie := new(http.Cookie)
|
|
|
|
cookie.Name = domain.CookieToken
|
|
|
|
cookie.Value = resp.Token
|
|
|
|
c.SetCookie(cookie)
|
|
|
|
|
|
|
|
cookie = new(http.Cookie)
|
|
|
|
cookie.Name = domain.CookieRefreshToken
|
|
|
|
cookie.Value = resp.RefreshToken
|
|
|
|
c.SetCookie(cookie)
|
|
|
|
|
|
|
|
cookie = new(http.Cookie)
|
|
|
|
cookie.Name = domain.CookieUser
|
|
|
|
cookie.Value = user
|
|
|
|
c.SetCookie(cookie)
|
|
|
|
|
|
|
|
return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true))
|
|
|
|
}
|
2024-05-12 09:35:13 -07:00
|
|
|
|
|
|
|
func (h *Handler) UserSignUp(c echo.Context) error {
|
|
|
|
return Render(c, http.StatusOK, users.SignUp())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) UserAfterSignUp(c echo.Context) error {
|
|
|
|
user := c.FormValue("username")
|
|
|
|
password := c.FormValue("password")
|
|
|
|
|
|
|
|
resp, err := h.api.Users.SignUp(user, password)
|
|
|
|
if err != nil {
|
|
|
|
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
|
|
|
|
}
|
|
|
|
if resp.Message != "OK" {
|
|
|
|
msg := fmt.Sprintf("Failed to create account. Message: %s", resp.Message)
|
|
|
|
return Render(c, http.StatusBadRequest, users.AfterLogin(msg, false))
|
|
|
|
}
|
|
|
|
return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true))
|
|
|
|
}
|