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

about the router #26005

Closed
jianjianxu opened this issue Jul 20, 2023 · 3 comments · Fixed by #26105
Closed

about the router #26005

jianjianxu opened this issue Jul 20, 2023 · 3 comments · Fixed by #26105
Labels
good first issue Likely to be an easy fix type/proposal The new feature has not been accepted yet but needs to be discussed first.

Comments

@jianjianxu
Copy link

Feature Description

When I am not logged in, I open the webpage "https://gitea.com/user/login?redirect_to=https://gitea.com/work/myjob/issues/15", after logging in, it will redirect normally.

But, when I am already logged in, I open the webpage "https://gitea.com/user/login?redirect_to=https://gitea.com/work/myjob/issues/15", after logging in, I will be redirected to the homepage.

Screenshots

No response

@jianjianxu jianjianxu added the type/proposal The new feature has not been accepted yet but needs to be discussed first. label Jul 20, 2023
@lunny
Copy link
Member

lunny commented Jul 20, 2023

What's your suggestion?

@wxiaoguang wxiaoguang added the good first issue Likely to be an easy fix label Jul 24, 2023
@cassiozareck
Copy link
Contributor

cassiozareck commented Jul 24, 2023

Hello @lunny, @wxiaoguang and @jianjianxu ! I suppose the expected behaviour would be to redirect to the desired route in case he's logged in (in this case gitea.com/work/myjob/issues/15)

I suspect the issue lies in services/auth/middleware.go:

// Redirect to dashboard if user tries to visit any non-login page.
if options.SignOutRequired && ctx.IsSigned && ctx.Req.URL.RequestURI() != "/" {
	ctx.Redirect(setting.AppSubURL + "/")
	return
} 

This code redirects a logged-in user to the homepage if they try to access a route that requires sign-out. However, it doesn't consider the case where the user is trying to access the login page with a redirect_to parameter.

I think I will modify the middleware to account for the redirect_to case. I'll make a PR if it solve the issue

However, the checkAutoLogin function in routes/web/auth.go isn't being called. This function seems to handle the redirect_to paramater if user is signed in. Im new to codebase so I dont know if there's specific cases where it is suppose to get called

func checkAutoLogin(ctx *context.Context) bool {
	// Check auto-login
	isSucceed, err := AutoSignIn(ctx)
	if err != nil {
		ctx.ServerError("AutoSignIn", err)
		return true
	}

	redirectTo := ctx.FormString("redirect_to")
	if len(redirectTo) > 0 {
		middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
	} else {
		redirectTo = ctx.GetSiteCookie("redirect_to")
	}

	if isSucceed {
		middleware.DeleteRedirectToCookie(ctx.Resp)
		ctx.RedirectToFirst(redirectTo, setting.AppSubURL+string(setting.LandingPageURL))
		return true
	}

	return false
}

lunny pushed a commit that referenced this issue Jul 27, 2023
This PR addresses an issue where logged-in users get redirected to the
homepage when trying to access a URL with the redirect_to parameter. The
issue was traced back to a middleware function in
services/auth/middleware.go that redirects logged-in users to the
homepage. This function didn't account the redirect_to parameter.

The fix modifies the middleware function to check for this case and
redirect the user to the specified URL instead of the homepage.

Closes: #26005

---------

Signed-off-by: cassiozareck <cassiomilczareck@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
@wxiaoguang
Copy link
Contributor

Many thanks to @cassiozareck for addressing the problem.

@jianjianxu could you try it on try.gitea.io (eg: https://try.gitea.io/user/login?redirect_to=/issues ) to see whether #26105 works? I made some changes there because it seems that the "POST" method is not a must for this issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Likely to be an easy fix type/proposal The new feature has not been accepted yet but needs to be discussed first.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants