Search code examples
gobasic-authenticationgo-gin

Get BasicAuth creds in Gin


I have made a switch to Gin to try it out. Before the move I accessed the BasicAuth credentials (app_id and token) using the request object like this:

appId, token, _ := r.BasicAuth()

The app_id needs to be found in my database on every call so I'm using Gin middleware for this:

func CheckAppId() gin.HandlerFunc {
    return func(c *gin.Context) {
        //how do I access the BasicAuth creds here?
    }
}

but I'm not sure how to access the BasicAuth creds without the request object.


Solution

  • The gin context contains the http.Request object in the Request field.