Add setting userID from token as in middelware

This commit is contained in:
qowevisa 2024-08-03 08:50:06 +03:00
parent 2d61d59179
commit 63c96edc88

View File

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
)
// Passes UserID with `c.Set("UserID")` as it gets id from token
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
@ -22,6 +23,14 @@ func AuthMiddleware() gin.HandlerFunc {
return
}
if userID, err := tokens.GetID(token); err != nil {
c.JSON(401, types.ErrorResponse{Message: "Token is invalid ERR4001"})
c.Abort()
return
} else {
c.Set("UserID", userID)
}
c.Next()
}
}