- Updated middleware to fetch user and tenant models by ID and set them in context. - Refactored common service methods to accept userID as a parameter instead of extracting from context. - Modified content service methods to include userID as a parameter for better clarity and performance. - Adjusted coupon, creator, notification, order, tenant, user, and wallet services to utilize userID directly. - Enhanced context key constants for improved readability and maintainability.
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"quyun/v2/app/errorx"
|
|
"quyun/v2/app/services"
|
|
"quyun/v2/pkg/consts"
|
|
"quyun/v2/providers/jwt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Middlewares provides reusable Fiber middlewares shared across modules.
|
|
//
|
|
// @provider
|
|
type Middlewares struct {
|
|
// log is the module logger injected by the framework.
|
|
log *log.Entry `inject:"false"`
|
|
// jwt is the JWT provider used by auth-related middlewares.
|
|
jwt *jwt.JWT
|
|
}
|
|
|
|
func (f *Middlewares) Prepare() error {
|
|
f.log = log.WithField("module", "middleware")
|
|
return nil
|
|
}
|
|
|
|
func (m *Middlewares) Auth(ctx fiber.Ctx) error {
|
|
authHeader := ctx.Get("Authorization")
|
|
if authHeader == "" {
|
|
return ctx.Next()
|
|
}
|
|
|
|
claims, err := m.jwt.Parse(authHeader)
|
|
if err != nil {
|
|
return errorx.ErrUnauthorized.WithCause(err).WithMsg("Invalid token")
|
|
}
|
|
|
|
// get user model
|
|
user, err := services.User.GetModelByID(ctx.Context(), claims.UserID)
|
|
if err != nil {
|
|
return errorx.ErrUnauthorized.WithCause(err).WithMsg("UserNotFound")
|
|
}
|
|
|
|
// Set Context
|
|
ctx.Locals(consts.CtxKeyUser, user)
|
|
if claims.TenantID > 0 {
|
|
tenant, err := services.Tenant.GetModelByID(ctx, claims.TenantID)
|
|
if err != nil {
|
|
return errorx.ErrUnauthorized.WithCause(err).WithMsg("TenantNotFound")
|
|
}
|
|
ctx.Locals(consts.CtxKeyTenant, tenant)
|
|
}
|
|
|
|
return ctx.Next()
|
|
}
|