feat: Refactor user context handling and service methods
- 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.
This commit is contained in:
@@ -2,6 +2,7 @@ package middlewares
|
||||
|
||||
import (
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/pkg/consts"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
@@ -35,10 +36,20 @@ func (m *Middlewares) Auth(ctx fiber.Ctx) error {
|
||||
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, claims.UserID)
|
||||
ctx.Locals(consts.CtxKeyUser, user)
|
||||
if claims.TenantID > 0 {
|
||||
ctx.Locals(consts.CtxKeyTenant, claims.TenantID)
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user