📌  相关文章
📜  gin 中间件示例 - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:45:02.233000             🧑  作者: Mango

代码示例1
func Auth() gin.HandlerFunc {

    return gin.HandlerFunc(func(ctx *gin.Context) {
        SecretPublicKey := utils.GodotEnv("JWT_SECRET")
        token, err := utils.Verify(ctx, SecretPublicKey)

        response := UnathorizatedResponse{
            Status:  "Unathorizated",
            Code:    http.StatusUnauthorized,
            Method:  ctx.Request.Method,
            Message: "accessToken invalid or expired",
        }

        if err != nil {
            ctx.JSON(http.StatusUnauthorized, response)
        } else {
            // global value result
            ctx.Set("user", token.Claims)
            // return to next method if token is exist
            ctx.Next()
        }
    })
}