This commit is contained in:
Lizandro Guarnizo
2026-05-16 13:44:32 -05:00
parent 85595e639e
commit 76a0c28b16
16 changed files with 225 additions and 725 deletions
+3 -23
View File
@@ -2,7 +2,6 @@ package auth
import (
"errors"
"log"
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/fiber-boilerplate/app"
@@ -15,20 +14,11 @@ const portalSessionKey = "portal_user_id"
func PortalUser(c *fiber.Ctx) (*models.PortalUser, error) {
store := app.Http.Session.Get(c)
raw := store.Get(portalSessionKey)
log.Printf("[PORTAL SESSION] GET %s → cookie=%q raw=%v", c.Path(), c.Cookies("session_id"), raw)
if raw == nil {
return nil, errors.New("no portal session")
}
var id uint
switch v := raw.(type) {
case uint:
id = v
case int64:
id = uint(v)
case float64:
id = uint(v)
default:
log.Printf("[PORTAL SESSION] type assertion failed: %T", raw)
id, ok := raw.(uint)
if !ok {
return nil, errors.New("invalid portal session")
}
u, err := models.GetPortalUserByID(id)
@@ -45,18 +35,8 @@ func PortalUser(c *fiber.Ctx) (*models.PortalUser, error) {
// SetPortalSession guarda el portal_user_id en la sesión.
func SetPortalSession(c *fiber.Ctx, userID uint) error {
store := app.Http.Session.Get(c)
// Regenerar ID de sesión para evitar session fixation
if err := store.Regenerate(); err != nil {
log.Printf("[PORTAL SESSION] Regenerate error: %v", err)
return err
}
store.Set(portalSessionKey, userID)
if err := store.Save(); err != nil {
log.Printf("[PORTAL SESSION] Save error: %v", err)
return err
}
log.Printf("[PORTAL SESSION] SET userID=%d cookie=%q", userID, c.Cookies("session_id"))
return nil
return store.Save()
}
// DestroyPortalSession elimina la sesión del portal.