Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] - How to redirect to different url in middleware? #414

Closed
sujit-baniya opened this issue Nov 23, 2022 · 8 comments
Closed

[QUESTION] - How to redirect to different url in middleware? #414

sujit-baniya opened this issue Nov 23, 2022 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@sujit-baniya
Copy link

sujit-baniya commented Nov 23, 2022

I'm trying to redirect to different internal url in middleware, but I'm not redirected. This is the code I tried.

package main

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

func WebAuth(redirectTo string) app.HandlerFunc {
	if redirectTo == "" {
		redirectTo = "/"
	}
	return func(c context.Context, ctx *app.RequestContext) {
		if _, ok := ctx.Get("user"); !ok {
			ctx.Redirect(consts.StatusFound, []byte(redirectTo))
			// ctx.JSON(200, "Test") Don't work either
			return
		}
		ctx.Next(c)
	}
}

func main() {
	h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
	g1 := h.Group("/")
	g1.Use(WebAuth("/foo"))
	g1.GET("/", func(ctx context.Context, c *app.RequestContext) {
		c.Set("user", 1)
		c.String(consts.StatusOK, "Home")
	})
	h.GET("/foo", func(ctx context.Context, c *app.RequestContext) {
		c.String(consts.StatusOK, "hello, world")
	})

	h.Spin()
}

The code enters the middleware and inside the if for redirection when tried to visit "/", but it's not redirecting to "/foo" and ends up with "Home" response

Expected Result:
The page should have redirected to "/foo" instead of going inside "/"

Please suggest what am I doing wrong

@li-jin-gou
Copy link
Member

cc @zstone12

@li-jin-gou li-jin-gou added the question Further information is requested label Nov 23, 2022
@sujit-baniya
Copy link
Author

@li-jin-gou In middleware whatever I put, ctx.JSON(), ctx.HTML(), ctx.Redirect(), the code is going inside "/" which is not expected

@zstone12
Copy link
Contributor

zstone12 commented Nov 23, 2022

func WebAuth(redirectTo string) app.HandlerFunc {
	if redirectTo == "" {
		redirectTo = "/"
	}
	return func(c context.Context, ctx *app.RequestContext) {
		if _, ok := ctx.Get("user"); !ok {
			ctx.Redirect(consts.StatusFound, []byte(redirectTo))
			ctx.Abort()
		}
		ctx.Next(c)
	}
}

Should use ctx.Abort().
Abort() prevents pending handlers from being called.

@li-jin-gou
Copy link
Member

li-jin-gou commented Nov 23, 2022

@sujit-baniya
Copy link
Author

Ah! got it. So wouldn't it be good if Abort() is called inside Redirect(). I mean what would be the use of Redirect() if Abort() is not called?

@zstone12
Copy link
Contributor

image
If not execute Abort(),it will cotinue execute the business handler,so the code is going inside "/" .

@sujit-baniya
Copy link
Author

Thanks for explanation. I was just wondering any use case where I would only call Redirect() and not Abort()

@li-jin-gou
Copy link
Member

Thanks for explanation. I was just wondering any use case where I would only call Redirect() and not Abort()

image

here last handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

3 participants