GetByID retrieves a session by its ID from the storage. If the session is not found, it returns nil and an error. Unlike session middleware methods, this function does not automatically: - Load the session into the request context. - Save the session data to the storage or update the client cooki
(ctx context.Context, id string)
| 300 | // // handle error |
| 301 | // } |
| 302 | func (s *Store) GetByID(ctx context.Context, id string) (*Session, error) { |
| 303 | if id == "" { |
| 304 | return nil, ErrEmptySessionID |
| 305 | } |
| 306 | |
| 307 | rawData, err := s.Storage.GetWithContext(ctx, id) |
| 308 | if err != nil { |
| 309 | return nil, err |
| 310 | } |
| 311 | if rawData == nil { |
| 312 | return nil, ErrSessionIDNotFoundInStore |
| 313 | } |
| 314 | |
| 315 | sess := acquireSession() |
| 316 | |
| 317 | sess.mu.Lock() |
| 318 | |
| 319 | sess.config = s |
| 320 | sess.id = id |
| 321 | sess.isFresh = false |
| 322 | |
| 323 | sess.data.Lock() |
| 324 | decodeErr := sess.decodeSessionData(rawData) |
| 325 | sess.data.Unlock() |
| 326 | sess.mu.Unlock() |
| 327 | if decodeErr != nil { |
| 328 | sess.Release() |
| 329 | return nil, fmt.Errorf("failed to decode session data: %w", decodeErr) |
| 330 | } |
| 331 | |
| 332 | if s.AbsoluteTimeout > 0 { |
| 333 | if sess.isAbsExpired() { |
| 334 | if err := sess.Destroy(); err != nil { //nolint:contextcheck // it is not right |
| 335 | sess.Release() |
| 336 | log.Errorf("failed to destroy session: %v", err) |
| 337 | } |
| 338 | return nil, ErrSessionIDNotFoundInStore |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return sess, nil |
| 343 | } |