Skip to content

Commit

Permalink
manager: simplify load logic
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Mar 20, 2024
1 parent c6b4eba commit 4d9dbe4
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,19 @@ func isValidSessionID(sid string, idLength int) bool {
}

// load loads the session from the session store with session ID provided in the
// named cookie. If a session with the ID does not exist, it creates a new
// session with the same ID. A boolean value is returned to indicate whether a
// new session is created.
// named cookie. It returns `created=true` if a new session is created.
func (m *manager) load(r *http.Request, sid string, idLength int) (_ Session, created bool, err error) {
if isValidSessionID(sid, idLength) && m.store.Exist(r.Context(), sid) {
sess, err := m.store.Read(r.Context(), sid)
if !isValidSessionID(sid, idLength) {
sid, err = randomChars(idLength)
if err != nil {
return nil, false, errors.Wrap(err, "read")
return nil, false, errors.Wrap(err, "new ID")

Check warning on line 126 in manager.go

View check run for this annotation

Codecov / codecov/patch

manager.go#L126

Added line #L126 was not covered by tests
}
return sess, false, nil
}

sid, err = randomChars(idLength)
if err != nil {
return nil, false, errors.Wrap(err, "new ID")
created = true
}

sess, err := m.store.Read(r.Context(), sid)
if err != nil {
return nil, false, errors.Wrap(err, "read")
}
return sess, true, nil
return sess, created, nil
}

0 comments on commit 4d9dbe4

Please sign in to comment.