MW usage refactor
This commit is contained in:
parent
229eebcf66
commit
21b54c4167
9 changed files with 98 additions and 48 deletions
|
|
@ -3,23 +3,35 @@ 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")
|
||||
type DepsMW struct {
|
||||
UserProv UserProvider
|
||||
AuthProv AuthChecker
|
||||
ServiceId int32
|
||||
}
|
||||
|
||||
type Middlewares struct {
|
||||
AuthMW gin.HandlerFunc
|
||||
RegMW gin.HandlerFunc
|
||||
}
|
||||
|
||||
func AuthMW(up UserProvider, ap AuthChecker, sid int32) gin.HandlerFunc {
|
||||
log.Debug("Auth Middlewares enabled")
|
||||
return func(c *gin.Context) {
|
||||
cookie, err := c.Cookie("sessionToken")
|
||||
cookie, err := c.Cookie(tokenCookieName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
userUuid, err := auth.VerifySession(c, cookie, serviceId)
|
||||
userUuid, err := ap.VerifySession(c, cookie, sid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
userId, err := up.GetUserId(c, userUuid)
|
||||
if err != nil {
|
||||
|
|
@ -33,3 +45,24 @@ func authMW(up user.Provider, auth authCheck.AuthChecker, serviceId int32) gin.H
|
|||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterMW(ap AuthChecker, sid int32) gin.HandlerFunc {
|
||||
log.Debug("Auth Middlewares enabled")
|
||||
return func(c *gin.Context) {
|
||||
cookie, err := c.Cookie(tokenCookieName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
userUuid, err := ap.VerifySession(c, cookie, sid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
c.Set("userUuid", userUuid)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue