Skip to content

How to Perform Redirects in Gofr? #1531

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

Open
ibadi-id opened this issue Feb 27, 2025 · 3 comments · May be fixed by #1640
Open

How to Perform Redirects in Gofr? #1531

ibadi-id opened this issue Feb 27, 2025 · 3 comments · May be fixed by #1640
Assignees
Labels
enhancement New feature or request

Comments

@ibadi-id
Copy link

I am trying to redirect users to a frontend URL after authentication in Gofr. However, I couldn't find a built-in Redirect method in ctx.

Here’s my current code:

app.GET("/auth/google/callback", func(ctx *gofr.Context) (interface{}, error) {
	sessionManager := session.GetSession()

	email := sessionManager.GetString(ctx.Context, "email")
	name := sessionManager.GetString(ctx.Context, "name")

	// Expected to redirect but ctx does not have a Redirect method
	frontendURL := fmt.Sprintf("https://frontend.com/dashboard?email=%s&name=%s", email, name)

	// What is the correct way to redirect?
	ctx.Redirect(frontendURL, http.StatusFound) // This does not exist in Gofr

	return nil, nil
})

Expected Behavior
A way to redirect users to the frontend URL after authentication.

Actual Behavior
ctx.Redirect() does not exist in Gofr. like in echo or Fiber
There is no clear documentation on how to handle redirects.

Questions
What is the correct way to redirect users in Gofr?
Is there an alternative method to set the Location header?

@Umang01-hash Umang01-hash added the enhancement New feature or request label Feb 27, 2025
@Umang01-hash Umang01-hash self-assigned this Feb 27, 2025
@Umang01-hash
Copy link
Member

@ibadi-id GoFr doesn't support Redirects as of now. Will definitely let you know once we add it.

@Umang01-hash
Copy link
Member

@ibadi-id Could you share more details on your use case for redirects? Understanding how you intend to use them will help us evaluate whether this should be part of GoFr or handled at the application level.

@ibadi-id
Copy link
Author

Thank you for your response, @Umang01-hash

My use case is that after receiving a callback from Google as an authentication provider, I want to create a user in the backend and then redirect the user to a specific frontend page once the process is complete.

Since GoFr currently does not support redirects, I have implemented a temporary workaround by adding a function to retrieve http.ResponseWriter, allowing me to perform a redirect like this:

func (c *Context) GetWriter() http.ResponseWriter {
	if httpResponder, ok := c.responder.(*gofwhttp.Responder); ok {
		return httpResponder.GetWriter()
	}
	return nil
}

func CallbackAuth(ctx *gofw.Context) (interface{}, error) {
	// Redirect to the frontend after successful login
	redirectURL := fmt.Sprintf(os.Getenv("REDIRECT_FRONTEND_URL"))
	w := ctx.GetWriter()
        if writer != nil {
           http.Redirect(w, &http.Request{}, redirectURL, http.StatusFound)
        }
	return nil, nil
}

However, if there is a better way to handle redirects in GoFr or if there are plans to support this feature in the future, I would greatly appreciate any suggestions or insights.

Thanks! 😊

@Umang01-hash Umang01-hash linked a pull request Apr 10, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants