api/internal/router/middleware.go

48 lines
1.2 KiB
Go
Raw Normal View History

2025-07-07 17:46:31 +03:00
package router
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"merch-parser-api/pkg/responses"
"net/http"
)
2025-09-10 21:57:09 +03:00
func (r *router) AuthMW() gin.HandlerFunc {
2025-07-07 17:46:31 +03:00
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
}
2025-09-10 21:57:09 +03:00
userUuid, err := r.tokenProv.Parse(token)
2025-07-07 17:46:31 +03:00
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)
2025-09-10 21:57:09 +03:00
log.WithField("userUuid", userUuid).Debug("MW | User uuid")
2025-07-07 17:46:31 +03:00
2025-09-10 21:57:09 +03:00
c.Next()
2025-07-07 17:46:31 +03:00
}
}
2025-09-14 19:33:09 +03:00
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()
}
}