api/internal/router/middleware.go
2025-09-14 19:33:09 +03:00

47 lines
1.2 KiB
Go

package router
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"merch-parser-api/pkg/responses"
"net/http"
)
func (r *router) AuthMW() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
if token == "" {
c.JSON(http.StatusUnauthorized, responses.ErrorResponse401{Error: "Authorization token is required"})
log.WithField("msg", "Authorization token is required").Error("MW | Authorization")
c.Abort()
return
}
userUuid, err := r.tokenProv.Parse(token)
if err != nil {
c.JSON(http.StatusUnauthorized, responses.ErrorResponse401{Error: err.Error()})
log.WithField("msg", "error parsing jwt").Error("MW | Authorization")
c.Abort()
return
}
c.Set("userUuid", userUuid)
log.WithField("userUuid", userUuid).Debug("MW | User uuid")
c.Next()
}
}
func (r *router) RefreshMW() gin.HandlerFunc {
return func(c *gin.Context) {
cookie, err := c.Request.Cookie("refresh_uuid")
if err != nil {
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
log.WithError(err).Error("User | Failed to get refresh cookie")
return
}
c.Set("refresh_uuid", cookie.Value)
c.Next()
}
}