2024-06-22 05:04:21 +00:00
|
|
|
package exts
|
2024-01-26 17:11:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-04-20 17:33:42 +00:00
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/sujit-baniya/flash"
|
2024-01-26 17:11:32 +00:00
|
|
|
)
|
|
|
|
|
2024-01-31 13:16:54 +00:00
|
|
|
var validation = validator.New(validator.WithRequiredStructEnabled())
|
2024-01-26 17:11:32 +00:00
|
|
|
|
|
|
|
func BindAndValidate(c *fiber.Ctx, out any) error {
|
|
|
|
if err := c.BodyParser(out); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else if err := validation.Struct(out); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-20 17:33:42 +00:00
|
|
|
|
|
|
|
func GetRedirectUri(c *fiber.Ctx, fallback ...string) *string {
|
|
|
|
if len(c.Query("redirect_uri")) > 0 {
|
|
|
|
return lo.ToPtr(c.Query("redirect_uri"))
|
|
|
|
} else if val, ok := flash.Get(c)["redirect_uri"].(*string); ok {
|
|
|
|
return val
|
|
|
|
} else if len(fallback) > 0 {
|
|
|
|
return &fallback[0]
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|