init push

This commit is contained in:
2026-07-16 22:16:45 +07:00
commit 8b068bdb10
1021 changed files with 332816 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package middleware
import (
"github.com/gofiber/fiber/v2"
"wucher/internal/service"
"wucher/internal/shared/pkg/appctx"
"wucher/internal/shared/pkg/apperrorsx"
)
func AuthRequired(svc *service.AuthService) fiber.Handler {
return func(c *fiber.Ctx) error {
if svc == nil {
e := apperrorsx.New(apperrorsx.ErrUserUnauthorized)
e.Message = "auth service not configured"
return apperrorsx.Write(c, e)
}
ctx := c.UserContext()
for _, candidate := range service.CookieValues(c.Get(fiber.HeaderCookie), svc.AccessCookieName()) {
userID, err := svc.ParseAccessToken(ctx, candidate)
if err == nil {
c.Locals("user_id", userID)
c.SetUserContext(appctx.WithUserID(ctx, userID))
return c.Next()
}
}
return apperrorsx.Write(c, apperrorsx.New(apperrorsx.ErrTokenInvalid))
}
}