From 7f3fdfb4be6381cc9a5fd0605c0fe45656bb4b1d Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Mon, 4 May 2026 22:22:25 -0500 Subject: [PATCH] up --- pkg/auth/user.go | 39 +++++++++++++++++++++++++++++---------- rest/middlewares/menu.go | 5 +++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/pkg/auth/user.go b/pkg/auth/user.go index 726cf72..9a3be9d 100755 --- a/pkg/auth/user.go +++ b/pkg/auth/user.go @@ -15,8 +15,16 @@ import ( func User(c *fiber.Ctx) (*models.Users, error) { store := app.Http.Session.Get(c) userID := store.Get("user_id") + + // Si no hay sesión activa, intentar reconstruirla desde el JWT if userID == nil { - return nil, errors.New("User Not Logged In") + id, err := app.Http.Token.ParseToken(c, app.Http.Token.AppJwtSecret) + if err != nil || id == 0 { + return nil, errors.New("User Not Logged In") + } + store.Set("user_id", id) + store.Save() + userID = id } user, err := models.FindUserByID(userID) @@ -42,20 +50,31 @@ func UserID(c *fiber.Ctx) uint { func IsLoggedIn(c *fiber.Ctx) bool { store := app.Http.Session.Get(c) userID := store.Get("user_id") + + // Si no hay sesión, intentar reconstruirla desde el JWT if userID == nil { - DeleteSession(store) - c.ClearCookie() - return false + id, err := app.Http.Token.ParseToken(c, app.Http.Token.AppJwtSecret) + if err != nil || id == 0 { + DeleteSession(store) + c.ClearCookie() + return false + } + store.Set("user_id", id) + store.Save() + userID = id } + token := c.Cookies("Verify-Rest-Token") if token == "" { tokenHash := store.Get("user_token") - c.Cookie(&fiber.Cookie{ - Name: "Verify-Rest-Token", - Value: fmt.Sprintf("%s", tokenHash), - Secure: false, - HTTPOnly: true, - }) + if tokenHash != nil { + c.Cookie(&fiber.Cookie{ + Name: "Verify-Rest-Token", + Value: fmt.Sprintf("%s", tokenHash), + Secure: false, + HTTPOnly: true, + }) + } } return true diff --git a/rest/middlewares/menu.go b/rest/middlewares/menu.go index 83f94fd..d563e99 100755 --- a/rest/middlewares/menu.go +++ b/rest/middlewares/menu.go @@ -82,6 +82,11 @@ func MenuMiddleware(c *fiber.Ctx) error { // Opcional: adjuntar la lista de URLs al contexto c.Locals("urls", urls) + // Los administradores tienen acceso a todas las rutas + if user.IsAdmin { + return c.Next() + } + // Verificar si la URL de la solicitud está en la lista de URLs permitidas requestURL := c.Path()