- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
I'm trying to redirect users to login page if they are not authenticated . an if I'm in /note and want to redirect to /user/login it redirects me to /note/user/login
How to reproduce
func IsAuthenticated() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
fmt.Println("im here")
token, err := c.Cookie("token")
if err != nil {
c.Redirect(http.StatusFound, "/user/login")
return
}
u, timeout, err := user.UserByToken(token)
if err != nil {
c.Redirect(http.StatusFound, "/user/login")
return
}
if time.Now().After(*timeout){
c.Redirect(http.StatusFound, "/user/login")
return
}
c.Set("user", u)
}
}
func Routers(r *gin.Engine) {
note := r.Group("/note")
note.GET("/", middleware.IsAuthenticated(), Index)
}
```` func Index(c *gin.Context){ u, _ := c.Get("user")
notes, err := note.Find(u.(user.User).ID)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": err.Error(),
})
return
}
c.HTML(http.StatusOK, "list.html", gin.H{
"Title": "user list",
"Notes": notes,
})
return
}
## Expectations
<!-- Your expectation result of 'curl' command, like -->
/user/login
## Actual result
<!-- Actual result showing the problem -->
$ curl -i http://localhost:8080/note/ /note/user/login ```
Environment
- go version:
- gin version (or commit ref):
- operating system:
Comment From: linvis
sorry, not reproduce.
and c.Redirect
will not modify the url, just pass it to http.Redirect
, so your usage looks like right
could you have a try with curl -iL http://localhost:8080/note/
, and see what's the location in http header, and how it redirect?