This commit is contained in:
Lizandro Guarnizo
2026-05-04 22:22:25 -05:00
parent 4913fdf984
commit 7f3fdfb4be
2 changed files with 34 additions and 10 deletions
+19
View File
@@ -15,9 +15,17 @@ import (
func User(c *fiber.Ctx) (*models.Users, error) { func User(c *fiber.Ctx) (*models.Users, error) {
store := app.Http.Session.Get(c) store := app.Http.Session.Get(c)
userID := store.Get("user_id") userID := store.Get("user_id")
// Si no hay sesión activa, intentar reconstruirla desde el JWT
if userID == nil { if userID == nil {
id, err := app.Http.Token.ParseToken(c, app.Http.Token.AppJwtSecret)
if err != nil || id == 0 {
return nil, errors.New("User Not Logged In") return nil, errors.New("User Not Logged In")
} }
store.Set("user_id", id)
store.Save()
userID = id
}
user, err := models.FindUserByID(userID) user, err := models.FindUserByID(userID)
if err != nil { if err != nil {
@@ -42,14 +50,24 @@ func UserID(c *fiber.Ctx) uint {
func IsLoggedIn(c *fiber.Ctx) bool { func IsLoggedIn(c *fiber.Ctx) bool {
store := app.Http.Session.Get(c) store := app.Http.Session.Get(c)
userID := store.Get("user_id") userID := store.Get("user_id")
// Si no hay sesión, intentar reconstruirla desde el JWT
if userID == nil { if userID == nil {
id, err := app.Http.Token.ParseToken(c, app.Http.Token.AppJwtSecret)
if err != nil || id == 0 {
DeleteSession(store) DeleteSession(store)
c.ClearCookie() c.ClearCookie()
return false return false
} }
store.Set("user_id", id)
store.Save()
userID = id
}
token := c.Cookies("Verify-Rest-Token") token := c.Cookies("Verify-Rest-Token")
if token == "" { if token == "" {
tokenHash := store.Get("user_token") tokenHash := store.Get("user_token")
if tokenHash != nil {
c.Cookie(&fiber.Cookie{ c.Cookie(&fiber.Cookie{
Name: "Verify-Rest-Token", Name: "Verify-Rest-Token",
Value: fmt.Sprintf("%s", tokenHash), Value: fmt.Sprintf("%s", tokenHash),
@@ -57,6 +75,7 @@ func IsLoggedIn(c *fiber.Ctx) bool {
HTTPOnly: true, HTTPOnly: true,
}) })
} }
}
return true return true
} }
+5
View File
@@ -82,6 +82,11 @@ func MenuMiddleware(c *fiber.Ctx) error {
// Opcional: adjuntar la lista de URLs al contexto // Opcional: adjuntar la lista de URLs al contexto
c.Locals("urls", urls) 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 // Verificar si la URL de la solicitud está en la lista de URLs permitidas
requestURL := c.Path() requestURL := c.Path()