Skip to content

redirecting url not setting truly #2415

@sorushsaghari

Description

@sorushsaghari
  • 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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions