37 lines
863 B
Go
37 lines
863 B
Go
|
package middlewares
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
jwttoken "online-order/utils/jwt_token"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"online-order/domain"
|
||
|
)
|
||
|
|
||
|
type controller struct {
|
||
|
service domain.AuthService
|
||
|
}
|
||
|
|
||
|
func NewMiddlewareControllers(svc domain.AuthService) *controller {
|
||
|
return &controller{
|
||
|
service: svc,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (cont *controller) JwAuthtMiddleware() gin.HandlerFunc {
|
||
|
return func(c *gin.Context) {
|
||
|
user_id, err := jwttoken.IsTokenValid(c)
|
||
|
if err != nil {
|
||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized. Please Login first", "code": 401, "details": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
user, _, err := cont.service.GetByID(user_id)
|
||
|
if err != nil || user == nil {
|
||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized. User not found", "code": 401, "details": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
c.Next()
|
||
|
}
|
||
|
}
|