34 lines
871 B
Go
34 lines
871 B
Go
package router
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
log "github.com/sirupsen/logrus"
|
|
"merch-api/internal/user"
|
|
"merch-api/pkg/authCheck"
|
|
"merch-api/pkg/responses"
|
|
"net/http"
|
|
)
|
|
|
|
func authMW(up user.Provider, auth authCheck.AuthChecker, serviceId int32) gin.HandlerFunc {
|
|
log.Debug("Auth Middleware enabled")
|
|
return func(c *gin.Context) {
|
|
cookie, err := c.Cookie("sessionToken")
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
userUuid, err := auth.VerifySession(c, cookie, serviceId)
|
|
|
|
userId, err := up.GetUserId(c, userUuid)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()})
|
|
log.WithError(err).Errorf("%v error converting user uuid to user id: %v", pkgLogHeader, userUuid)
|
|
return
|
|
}
|
|
|
|
c.Set("userId", userId)
|
|
c.Next()
|
|
}
|
|
}
|