Answers for "gin middleware redirect"

Go
0

gin middleware redirect

//This is auth redirect example
func my_middleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		_, err := c.Request.Cookie("auth")
		if err == http.ErrNoCookie {
			c.Redirect(http.StatusTemporaryRedirect, "example.com/redirect")
			c.AbortWithStatus(http.StatusTemporaryRedirect)
		}
		if err != nil {
			// some other error
			http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
			c.AbortWithStatus(http.StatusInternalServerError)
		}
		c.Next()
	}
}
Posted by: Guest on June-12-2020

Browse Popular Code Answers by Language