mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-05-12 14:38:28 -05:00
Add redirect logic for first user creation and login pages
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user