package middleware

import (
	"gitea.pena/PenaSide/heruvym/internal/utils/jwt_adapter"
	"github.com/gofiber/fiber/v2"
)

func (mw *Middleware) MiddlewareGetJwt(c *fiber.Ctx) error {
	if c.Method() != fiber.MethodGet {
		return c.Next()
	}

	bearer := c.Query(jwt_adapter.DefaultHeaderKey)
	if bearer != "" {
		adapter, err := jwt_adapter.Decode(bearer)
		if err == nil {
			c.Locals(jwt_adapter.DefaultHeaderKey, adapter)
		}
	} else {
		sess := c.Query("s")
		if sess == "" {
			return nil
		}
		c.Locals(jwt_adapter.DefaultHeaderKey, &jwt_adapter.JwtAdapter{Id: sess})
	}

	return c.Next()
}
