Files
crawlab/backend/middlewares/auth.go
Marvin Zhang a16f296167 added jwt
2019-07-27 00:00:59 +08:00

29 lines
616 B
Go

package middlewares
import (
"crawlab/routes"
"crawlab/services"
"github.com/gin-gonic/gin"
"net/http"
)
func AuthorizationMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
tokenStr := c.GetHeader("Authorization")
if c.Request.URL.Path == "/login" || (c.Request.URL.Path == "/users" && c.Request.Method == "PUT") {
c.Next()
} else {
_, err := services.CheckToken(tokenStr)
if err == nil {
c.Next()
} else {
c.AbortWithStatusJSON(http.StatusUnauthorized, routes.Response{
Status: "ok",
Message: "unauthorized",
Error: "unauthorized",
})
}
}
}
}