up
This commit is contained in:
+29
-10
@@ -15,8 +15,16 @@ 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 {
|
||||||
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)
|
user, err := models.FindUserByID(userID)
|
||||||
@@ -42,20 +50,31 @@ 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 {
|
||||||
DeleteSession(store)
|
id, err := app.Http.Token.ParseToken(c, app.Http.Token.AppJwtSecret)
|
||||||
c.ClearCookie()
|
if err != nil || id == 0 {
|
||||||
return false
|
DeleteSession(store)
|
||||||
|
c.ClearCookie()
|
||||||
|
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")
|
||||||
c.Cookie(&fiber.Cookie{
|
if tokenHash != nil {
|
||||||
Name: "Verify-Rest-Token",
|
c.Cookie(&fiber.Cookie{
|
||||||
Value: fmt.Sprintf("%s", tokenHash),
|
Name: "Verify-Rest-Token",
|
||||||
Secure: false,
|
Value: fmt.Sprintf("%s", tokenHash),
|
||||||
HTTPOnly: true,
|
Secure: false,
|
||||||
})
|
HTTPOnly: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user