Skip to content

Instantly share code, notes, and snippets.

@undeadops
Created June 13, 2017 05:32
Show Gist options
  • Save undeadops/b16dc459f3951527930ab5274412e6a7 to your computer and use it in GitHub Desktop.
Save undeadops/b16dc459f3951527930ab5274412e6a7 to your computer and use it in GitHub Desktop.
gin MiddlewareTokenAuth
package main
// Custom Middleware for validating token
func TokenAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Request.Header.Get("X-Auth")
fmt.Println("X-Auth: ")
fmt.Println(token)
if token == "" {
respondWithError(401, "API token required", c)
c.Abort()
return
}
if token != "somethinglonghere" {
respondWithError(401, "Invalid API token", c)
c.Abort()
return
}
c.Next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment