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"
|
2024-07-07 08:00:43 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/models"
|
2024-05-12 09:22:14 -07:00
|
|
|
"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 {
|
2024-07-07 08:00:43 -07:00
|
|
|
return Render(c, http.StatusBadRequest, users.AfterLogin(models.AfterLoginViewModel{
|
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
}))
|
2024-05-12 09:22:14 -07:00
|
|
|
}
|
2024-05-18 22:38:31 -07:00
|
|
|
|
2024-06-02 19:55:05 -07:00
|
|
|
if user == "" {
|
|
|
|
user = "admin"
|
|
|
|
}
|
|
|
|
|
2024-05-18 22:38:31 -07:00
|
|
|
SetCookie(c, domain.CookieToken, resp.Token, "/")
|
|
|
|
SetCookie(c, domain.CookieRefreshToken, resp.RefreshToken, "/")
|
|
|
|
SetCookie(c, domain.CookieUser, user, "/")
|
2024-05-12 09:22:14 -07:00
|
|
|
|
2024-07-07 08:00:43 -07:00
|
|
|
vm := models.AfterLoginViewModel{
|
|
|
|
Success: true,
|
|
|
|
Message: "Login Successful!",
|
|
|
|
}
|
|
|
|
return Render(c, http.StatusOK, users.AfterLogin(vm))
|
2024-05-12 09:22:14 -07:00
|
|
|
}
|
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 {
|
2024-07-07 08:00:43 -07:00
|
|
|
return Render(c, http.StatusBadRequest, users.AfterLogin(models.AfterLoginViewModel{
|
|
|
|
Success: false,
|
|
|
|
Message: err.Error(),
|
|
|
|
}))
|
2024-05-12 09:35:13 -07:00
|
|
|
}
|
|
|
|
if resp.Message != "OK" {
|
|
|
|
msg := fmt.Sprintf("Failed to create account. Message: %s", resp.Message)
|
2024-07-07 08:00:43 -07:00
|
|
|
return Render(c, http.StatusBadRequest, users.AfterLogin(models.AfterLoginViewModel{
|
|
|
|
Message: msg,
|
|
|
|
Success: false,
|
|
|
|
}))
|
2024-05-12 09:35:13 -07:00
|
|
|
}
|
|
|
|
return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true))
|
2024-05-26 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) UserProfile(c echo.Context) error {
|
|
|
|
return Render(c, http.StatusOK, users.Profile())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ForceLogout(c echo.Context) error {
|
2024-07-07 08:00:43 -07:00
|
|
|
err := IsLoggedIn(c)
|
2024-05-26 19:30:04 -07:00
|
|
|
if err != nil {
|
2024-07-07 08:00:43 -07:00
|
|
|
return RenderError(c, err)
|
2024-05-26 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
h.api.Users.RefreshSessionToken(GetJwtToken(c))
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-02 19:55:05 -07:00
|
|
|
|
|
|
|
func (h *Handler) UsersLogout(c echo.Context) error {
|
|
|
|
SetCookie(c, domain.CookieUser, "", "/")
|
|
|
|
SetCookie(c, domain.CookieRefreshToken, "", "/")
|
|
|
|
SetCookie(c, domain.CookieToken, "", "/")
|
|
|
|
|
|
|
|
return Render(c, http.StatusOK, users.Logout())
|
|
|
|
}
|