adding a little more view models

This commit is contained in:
James Tombleson 2024-07-07 08:00:43 -07:00
parent ed1ca5831a
commit f5cbdc115b
3 changed files with 34 additions and 13 deletions

View File

@ -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))

6
internal/models/users.go Normal file
View File

@ -0,0 +1,6 @@
package models
type AfterLoginViewModel struct {
Message string
Success bool
}

View File

@ -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 {
<div class="notification is-success">
{ message }
{ vm.Message }
</div>
} else {
<div class="notification is-error">
{ message }
{ vm.Message }
</div>
}
}