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
+29 -10
View File
@@ -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
+5
View File
@@ -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()