Add redirect logic for first user creation and login pages

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-22 23:25:29 -06:00
parent e64299e5ce
commit e649de025f
2 changed files with 31 additions and 0 deletions
@@ -5,6 +5,7 @@ import (
lucide "github.com/eduardolat/gomponents-lucide"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/eduardolat/pgbackweb/internal/logger"
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/validate"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
@@ -16,6 +17,21 @@ import (
)
func (h *handlers) createFirstUserPageHandler(c echo.Context) error {
ctx := c.Request().Context()
usersQty, err := h.servs.UsersService.GetUsersQty(ctx)
if err != nil {
logger.Error("failed to get users qty", logger.KV{
"ip": c.RealIP(),
"ua": c.Request().UserAgent(),
"error": err,
})
return c.String(http.StatusInternalServerError, "Internal server error")
}
if usersQty > 0 {
return c.Redirect(http.StatusFound, "/auth/login")
}
return echoutil.RenderGomponent(c, http.StatusOK, createFirstUserPage())
}
+15
View File
@@ -16,6 +16,21 @@ import (
)
func (h *handlers) loginPageHandler(c echo.Context) error {
ctx := c.Request().Context()
usersQty, err := h.servs.UsersService.GetUsersQty(ctx)
if err != nil {
logger.Error("failed to get users qty", logger.KV{
"ip": c.RealIP(),
"ua": c.Request().UserAgent(),
"error": err,
})
return c.String(http.StatusInternalServerError, "Internal server error")
}
if usersQty == 0 {
return c.Redirect(http.StatusFound, "/auth/create-first-user")
}
return echoutil.RenderGomponent(c, http.StatusOK, loginPage())
}