Update portal.go
This commit is contained in:
+10
-1
@@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||||
@@ -14,11 +15,13 @@ const portalSessionKey = "portal_user_id"
|
|||||||
func PortalUser(c *fiber.Ctx) (*models.PortalUser, error) {
|
func PortalUser(c *fiber.Ctx) (*models.PortalUser, error) {
|
||||||
store := app.Http.Session.Get(c)
|
store := app.Http.Session.Get(c)
|
||||||
raw := store.Get(portalSessionKey)
|
raw := store.Get(portalSessionKey)
|
||||||
|
log.Printf("[PORTAL SESSION] GET %s → cookie=%q raw=%v", c.Path(), c.Cookies("session_id"), raw)
|
||||||
if raw == nil {
|
if raw == nil {
|
||||||
return nil, errors.New("no portal session")
|
return nil, errors.New("no portal session")
|
||||||
}
|
}
|
||||||
id, ok := raw.(uint)
|
id, ok := raw.(uint)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Printf("[PORTAL SESSION] type assertion failed: %T", raw)
|
||||||
return nil, errors.New("invalid portal session")
|
return nil, errors.New("invalid portal session")
|
||||||
}
|
}
|
||||||
u, err := models.GetPortalUserByID(id)
|
u, err := models.GetPortalUserByID(id)
|
||||||
@@ -37,10 +40,16 @@ func SetPortalSession(c *fiber.Ctx, userID uint) error {
|
|||||||
store := app.Http.Session.Get(c)
|
store := app.Http.Session.Get(c)
|
||||||
// Regenerar ID de sesión para evitar session fixation
|
// Regenerar ID de sesión para evitar session fixation
|
||||||
if err := store.Regenerate(); err != nil {
|
if err := store.Regenerate(); err != nil {
|
||||||
|
log.Printf("[PORTAL SESSION] Regenerate error: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
store.Set(portalSessionKey, userID)
|
store.Set(portalSessionKey, userID)
|
||||||
return store.Save()
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// DestroyPortalSession elimina la sesión del portal.
|
// DestroyPortalSession elimina la sesión del portal.
|
||||||
|
|||||||
Reference in New Issue
Block a user