Skip to content

Commit 82db9a2

Browse files
lunnywxiaoguang
andauthored
Fix the bug that user may logout if he switch pages too fast (#29962)
This PR fixed a bug when the user switching pages too fast, he will logout automatically. The reason is that when the error is context cancelled, the previous code think user hasn't login then the session will be deleted. Now it will return the errors but not think it's not login. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent b150ff0 commit 82db9a2

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

services/auth/session.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package auth
55

66
import (
7-
"context"
87
"net/http"
98

109
user_model "code.gitea.io/gitea/models/user"
@@ -29,40 +28,33 @@ func (s *Session) Name() string {
2928
// object for that uid.
3029
// Returns nil if there is no user uid stored in the session.
3130
func (s *Session) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
32-
user := SessionUser(req.Context(), sess)
33-
if user != nil {
34-
return user, nil
35-
}
36-
return nil, nil
37-
}
38-
39-
// SessionUser returns the user object corresponding to the "uid" session variable.
40-
func SessionUser(ctx context.Context, sess SessionStore) *user_model.User {
4131
if sess == nil {
42-
return nil
32+
return nil, nil
4333
}
4434

4535
// Get user ID
4636
uid := sess.Get("uid")
4737
if uid == nil {
48-
return nil
38+
return nil, nil
4939
}
5040
log.Trace("Session Authorization: Found user[%d]", uid)
5141

5242
id, ok := uid.(int64)
5343
if !ok {
54-
return nil
44+
return nil, nil
5545
}
5646

5747
// Get user object
58-
user, err := user_model.GetUserByID(ctx, id)
48+
user, err := user_model.GetUserByID(req.Context(), id)
5949
if err != nil {
6050
if !user_model.IsErrUserNotExist(err) {
61-
log.Error("GetUserById: %v", err)
51+
log.Error("GetUserByID: %v", err)
52+
// Return the err as-is to keep current signed-in session, in case the err is something like context.Canceled. Otherwise non-existing user (nil, nil) will make the caller clear the signed-in session.
53+
return nil, err
6254
}
63-
return nil
55+
return nil, nil
6456
}
6557

6658
log.Trace("Session Authorization: Logged in user %-v", user)
67-
return user
59+
return user, nil
6860
}

0 commit comments

Comments
 (0)