Skip to content

Commit 87ad09e

Browse files
committed
fix
1 parent 7ec4c65 commit 87ad09e

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

routers/web/auth/auth.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,21 @@ func resetLocale(ctx *context.Context, u *user_model.User) error {
123123
return nil
124124
}
125125

126+
func RedirectAfterLogin(ctx *context.Context) {
127+
redirectTo := ctx.FormString("redirect_to")
128+
if redirectTo == "" {
129+
redirectTo = ctx.GetSiteCookie("redirect_to")
130+
}
131+
middleware.DeleteRedirectToCookie(ctx.Resp)
132+
nextRedirectTo := setting.AppSubURL + string(setting.LandingPageURL)
133+
if setting.LandingPageURL == setting.LandingPageLogin {
134+
nextRedirectTo = setting.AppSubURL + "/" // do not cycle-redirect to the login page
135+
}
136+
ctx.RedirectToFirst(redirectTo, nextRedirectTo)
137+
}
138+
126139
func CheckAutoLogin(ctx *context.Context) bool {
127-
// Check auto-login
128-
isSucceed, err := autoSignIn(ctx)
140+
isSucceed, err := autoSignIn(ctx) // try to auto-login
129141
if err != nil {
130142
if errors.Is(err, auth_service.ErrAuthTokenInvalidHash) {
131143
ctx.Flash.Error(ctx.Tr("auth.remember_me.compromised"), true)
@@ -138,17 +150,10 @@ func CheckAutoLogin(ctx *context.Context) bool {
138150
redirectTo := ctx.FormString("redirect_to")
139151
if len(redirectTo) > 0 {
140152
middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
141-
} else {
142-
redirectTo = ctx.GetSiteCookie("redirect_to")
143153
}
144154

145155
if isSucceed {
146-
middleware.DeleteRedirectToCookie(ctx.Resp)
147-
nextRedirectTo := setting.AppSubURL + string(setting.LandingPageURL)
148-
if setting.LandingPageURL == setting.LandingPageLogin {
149-
nextRedirectTo = setting.AppSubURL + "/" // do not cycle-redirect to the login page
150-
}
151-
ctx.RedirectToFirst(redirectTo, nextRedirectTo)
156+
RedirectAfterLogin(ctx)
152157
return true
153158
}
154159

@@ -163,6 +168,11 @@ func SignIn(ctx *context.Context) {
163168
return
164169
}
165170

171+
if ctx.IsSigned {
172+
RedirectAfterLogin(ctx)
173+
return
174+
}
175+
166176
oauth2Providers, err := oauth2.GetOAuth2Providers(ctx, optional.Some(true))
167177
if err != nil {
168178
ctx.ServerError("UserSignIn", err)

routers/web/auth/auth_test.go

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

services/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
}

0 commit comments

Comments
 (0)