diff --git a/internal/handlers/users.go b/internal/handlers/users.go index b76311c..44bb90a 100644 --- a/internal/handlers/users.go +++ b/internal/handlers/users.go @@ -5,7 +5,7 @@ import ( "net/http" "git.jamestombleson.com/jtom38/newsbot-portal/internal/domain" - "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout" + "git.jamestombleson.com/jtom38/newsbot-portal/internal/models" "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/users" "github.com/labstack/echo/v4" ) @@ -20,7 +20,10 @@ func (h *Handler) UserAfterLogin(c echo.Context) error { resp, err := h.api.Users.Login(user, password) if err != nil { - return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false)) + return Render(c, http.StatusBadRequest, users.AfterLogin(models.AfterLoginViewModel{ + Success: false, + Message: err.Error(), + })) } if user == "" { @@ -31,7 +34,11 @@ func (h *Handler) UserAfterLogin(c echo.Context) error { SetCookie(c, domain.CookieRefreshToken, resp.RefreshToken, "/") SetCookie(c, domain.CookieUser, user, "/") - return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true)) + vm := models.AfterLoginViewModel{ + Success: true, + Message: "Login Successful!", + } + return Render(c, http.StatusOK, users.AfterLogin(vm)) } func (h *Handler) UserSignUp(c echo.Context) error { @@ -44,11 +51,17 @@ func (h *Handler) UserAfterSignUp(c echo.Context) error { resp, err := h.api.Users.SignUp(user, password) if err != nil { - return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false)) + return Render(c, http.StatusBadRequest, users.AfterLogin(models.AfterLoginViewModel{ + Success: false, + Message: err.Error(), + })) } 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.StatusBadRequest, users.AfterLogin(models.AfterLoginViewModel{ + Message: msg, + Success: false, + })) } return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true)) } @@ -58,9 +71,9 @@ func (h *Handler) UserProfile(c echo.Context) error { } func (h *Handler) ForceLogout(c echo.Context) error { - _, err := ValidateJwt(c, h.config.JwtSecret, h.config.ServerAddress) + err := IsLoggedIn(c) if err != nil { - return Render(c, http.StatusOK, layout.Error(err)) + return RenderError(c, err) } h.api.Users.RefreshSessionToken(GetJwtToken(c)) diff --git a/internal/models/users.go b/internal/models/users.go new file mode 100644 index 0000000..a06736a --- /dev/null +++ b/internal/models/users.go @@ -0,0 +1,6 @@ +package models + +type AfterLoginViewModel struct { + Message string + Success bool +} diff --git a/internal/views/users/afterLogin.templ b/internal/views/users/afterLogin.templ index 8025e9a..213f221 100644 --- a/internal/views/users/afterLogin.templ +++ b/internal/views/users/afterLogin.templ @@ -1,15 +1,17 @@ package users +import "git.jamestombleson.com/jtom38/newsbot-portal/internal/models" + // This is returned after the user logs into the application. // It just returns a partial view because it will overlap with the existing template. -templ AfterLogin(message string, success bool) { - if success { +templ AfterLogin(vm models.AfterLoginViewModel) { + if vm.Success {