You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting up an API and I'm very new to Go and Gin. I am able to return data from each URL until I attempt to integrate AuthRequired()
// User Functions - located in users.gouser:=r.Group("/user")
user.Use(AuthRequired())
{
user.GET("/info", userInfo)
user.PUT("/update", updateUser)
user.DELETE("/delete", deleteUser)
}
I'm assuming AuthRequired is calling a function. That function will then return success or fail based off the parameters provided. When a user is created or logged in, they get a token, that token + email, if found as a matching pair in the DB will grant them access.
//Authenticate UserfuncAuthRequired() gin.HandlerFunc {
returnfunc(c*gin.Context) {
//Get token and e-mail from headertoken:=c.Request.Header.Get("AuthToken")
email:=c.Request.Header.Get("AuthEmail")
//check to see if email & token were providediflen(token) ==0||len(email) ==0 {
}
//Find email in database//Compare stored token with token provided in header//Return - Authentication was success or fail
}
}
The text was updated successfully, but these errors were encountered:
Well, that is the best feature of Gin, you can integrate any middleware before a group of requests,
For example, if you have a group, "/v1" and then you use a middleware Gin Function, that one will execute before the request reach the main function you defined.
If something in that function, let's say a header check, goes wrong, you can throw a "c.Abort(403)", so the thread will not continue to your main defined function.
If your request header is OK, you should let that function finish, so the thread continues to the next middleware or your main defined function.
Thanks Javier,
You pretty much answered it. I wasn't sure if I was suppose to:
return false
Or how I was to signal a fail. I was also confused by the c.Next() but it found that in another thread and was able to piece it all together. Thanks for your help. I'm now able to authenticate users. :-)
Setting up an API and I'm very new to Go and Gin. I am able to return data from each URL until I attempt to integrate
AuthRequired()
I'm assuming
AuthRequired
is calling a function. That function will then return success or fail based off the parameters provided. When a user is created or logged in, they get a token, that token + email, if found as a matching pair in the DB will grant them access.The text was updated successfully, but these errors were encountered: