feat: add USER_ID_MIDDLEWARE constant and update AuthMiddleware to use it
This commit is contained in:
@@ -33,3 +33,7 @@ const (
|
|||||||
API_PATH_AUTH_LOGIN = "/login"
|
API_PATH_AUTH_LOGIN = "/login"
|
||||||
API_PATH_PROFILE = "/profile"
|
API_PATH_PROFILE = "/profile"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
USER_ID_MIDDLEWARE = "user_id"
|
||||||
|
)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"wm-backend/configs/constants"
|
||||||
"wm-backend/pkg/helper"
|
"wm-backend/pkg/helper"
|
||||||
"wm-backend/response"
|
"wm-backend/response"
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 5. Set user_id in gin context for downstream handlers
|
// 5. Set user_id in gin context for downstream handlers
|
||||||
c.Set("user_id", userID)
|
c.Set(constants.USER_ID_MIDDLEWARE, userID)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
"wm-backend/configs/constants"
|
||||||
"wm-backend/global"
|
"wm-backend/global"
|
||||||
"wm-backend/internal/models"
|
"wm-backend/internal/models"
|
||||||
"wm-backend/internal/models/requests"
|
"wm-backend/internal/models/requests"
|
||||||
@@ -29,6 +30,7 @@ import (
|
|||||||
// @Failure 500 {object} response.ErrorResponse
|
// @Failure 500 {object} response.ErrorResponse
|
||||||
// @Router /v1/invoices [post]
|
// @Router /v1/invoices [post]
|
||||||
func InvoiceCreate(c *gin.Context) error {
|
func InvoiceCreate(c *gin.Context) error {
|
||||||
|
userID, _ := c.Get(constants.USER_ID_MIDDLEWARE)
|
||||||
requestBody := requests.CreateInvoiceRequest{}
|
requestBody := requests.CreateInvoiceRequest{}
|
||||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||||
return nil
|
return nil
|
||||||
@@ -39,7 +41,7 @@ func InvoiceCreate(c *gin.Context) error {
|
|||||||
InvoiceConfigID: requestBody.InvoiceConfigID,
|
InvoiceConfigID: requestBody.InvoiceConfigID,
|
||||||
TotalItems: requestBody.TotalItems,
|
TotalItems: requestBody.TotalItems,
|
||||||
Note: requestBody.Note,
|
Note: requestBody.Note,
|
||||||
CreatedBy: requestBody.CreatedBy,
|
CreatedBy: userID.(string),
|
||||||
ApprovedBy: requestBody.ApprovedBy,
|
ApprovedBy: requestBody.ApprovedBy,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user