middleware gin code example
Example: gin middleware example
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 {
ctx.Set("user", token.Claims)
ctx.Next()
}
})
}