Created
June 13, 2017 05:32
-
-
Save undeadops/b16dc459f3951527930ab5274412e6a7 to your computer and use it in GitHub Desktop.
gin MiddlewareTokenAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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