2024-11-09 14:59:27 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/iris-contrib/middleware/jwt"
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
)
|
|
|
|
|
|
|
|
var AdminMiddleware = iris.Handler(func(ctx iris.Context) {
|
|
|
|
|
|
|
|
if token, ok := ctx.Values().Get("jwt").(*jwt.Token); ok {
|
|
|
|
// Use the token if needed
|
|
|
|
is_admin := token.Claims.(jwt.MapClaims)["is_admin"].(bool)
|
|
|
|
if !is_admin {
|
|
|
|
ctx.StatusCode(iris.StatusForbidden)
|
|
|
|
ctx.JSON(iris.Map{"message": "forbidden"})
|
2024-11-09 22:33:01 +08:00
|
|
|
return
|
2024-11-09 14:59:27 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.StatusCode(iris.StatusUnauthorized)
|
|
|
|
ctx.JSON(iris.Map{"message": "unauthorized"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Next()
|
|
|
|
|
|
|
|
})
|