Skip to content

Commit 02df269

Browse files
authored
Make "/user/login" page redirect if the current user has signed in (#29583) (#29599)
Backport #29583
1 parent 4ef7e49 commit 02df269

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

modules/contexttest/context_tests.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package contexttest
77
import (
88
gocontext "context"
99
"io"
10+
"maps"
1011
"net/http"
1112
"net/http/httptest"
1213
"net/url"
@@ -36,7 +37,7 @@ func mockRequest(t *testing.T, reqPath string) *http.Request {
3637
}
3738
requestURL, err := url.Parse(path)
3839
assert.NoError(t, err)
39-
req := &http.Request{Method: method, URL: requestURL, Form: url.Values{}}
40+
req := &http.Request{Method: method, URL: requestURL, Form: maps.Clone(requestURL.Query()), Header: http.Header{}}
4041
req = req.WithContext(middleware.WithContextData(req.Context()))
4142
return req
4243
}

routers/web/auth/auth.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,21 @@ func resetLocale(ctx *context.Context, u *user_model.User) error {
109109
return nil
110110
}
111111

112+
func RedirectAfterLogin(ctx *context.Context) {
113+
redirectTo := ctx.FormString("redirect_to")
114+
if redirectTo == "" {
115+
redirectTo = ctx.GetSiteCookie("redirect_to")
116+
}
117+
middleware.DeleteRedirectToCookie(ctx.Resp)
118+
nextRedirectTo := setting.AppSubURL + string(setting.LandingPageURL)
119+
if setting.LandingPageURL == setting.LandingPageLogin {
120+
nextRedirectTo = setting.AppSubURL + "/" // do not cycle-redirect to the login page
121+
}
122+
ctx.RedirectToFirst(redirectTo, nextRedirectTo)
123+
}
124+
112125
func checkAutoLogin(ctx *context.Context) bool {
113-
// Check auto-login
114-
isSucceed, err := AutoSignIn(ctx)
126+
isSucceed, err := AutoSignIn(ctx) // try to auto-login
115127
if err != nil {
116128
ctx.ServerError("AutoSignIn", err)
117129
return true
@@ -120,17 +132,10 @@ func checkAutoLogin(ctx *context.Context) bool {
120132
redirectTo := ctx.FormString("redirect_to")
121133
if len(redirectTo) > 0 {
122134
middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
123-
} else {
124-
redirectTo = ctx.GetSiteCookie("redirect_to")
125135
}
126136

127137
if isSucceed {
128-
middleware.DeleteRedirectToCookie(ctx.Resp)
129-
nextRedirectTo := setting.AppSubURL + string(setting.LandingPageURL)
130-
if setting.LandingPageURL == setting.LandingPageLogin {
131-
nextRedirectTo = setting.AppSubURL + "/" // do not cycle-redirect to the login page
132-
}
133-
ctx.RedirectToFirst(redirectTo, nextRedirectTo)
138+
RedirectAfterLogin(ctx)
134139
return true
135140
}
136141

@@ -146,6 +151,10 @@ func SignIn(ctx *context.Context) {
146151
return
147152
}
148153

154+
if ctx.IsSigned {
155+
RedirectAfterLogin(ctx)
156+
return
157+
}
149158
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(true)
150159
if err != nil {
151160
ctx.ServerError("UserSignIn", err)

routers/web/auth/auth_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package auth
5+
6+
import (
7+
"net/http"
8+
"net/url"
9+
"testing"
10+
11+
"code.gitea.io/gitea/modules/contexttest"
12+
"code.gitea.io/gitea/modules/test"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestUserLogin(t *testing.T) {
18+
ctx, resp := contexttest.MockContext(t, "/user/login")
19+
SignIn(ctx)
20+
assert.Equal(t, http.StatusOK, resp.Code)
21+
22+
ctx, resp = contexttest.MockContext(t, "/user/login")
23+
ctx.IsSigned = true
24+
SignIn(ctx)
25+
assert.Equal(t, http.StatusSeeOther, resp.Code)
26+
assert.Equal(t, "/", test.RedirectURL(resp))
27+
28+
ctx, resp = contexttest.MockContext(t, "/user/login?redirect_to=/other")
29+
ctx.IsSigned = true
30+
SignIn(ctx)
31+
assert.Equal(t, "/other", test.RedirectURL(resp))
32+
33+
ctx, resp = contexttest.MockContext(t, "/user/login")
34+
ctx.Req.AddCookie(&http.Cookie{Name: "redirect_to", Value: "/other-cookie"})
35+
ctx.IsSigned = true
36+
SignIn(ctx)
37+
assert.Equal(t, "/other-cookie", test.RedirectURL(resp))
38+
39+
ctx, resp = contexttest.MockContext(t, "/user/login?redirect_to="+url.QueryEscape("https://example.com"))
40+
ctx.IsSigned = true
41+
SignIn(ctx)
42+
assert.Equal(t, "/", test.RedirectURL(resp))
43+
}

routers/web/repo/wiki_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func assertPagesMetas(t *testing.T, expectedNames []string, metas any) {
7878
func TestWiki(t *testing.T) {
7979
unittest.PrepareTestEnv(t)
8080

81-
ctx, _ := contexttest.MockContext(t, "user2/repo1/wiki/?action=_pages")
81+
ctx, _ := contexttest.MockContext(t, "user2/repo1/wiki")
8282
ctx.SetParams("*", "Home")
8383
contexttest.LoadRepo(t, ctx, 1)
8484
Wiki(ctx)

0 commit comments

Comments
 (0)