Skip to content

Session manager listeners are called multiple times, even when nothing changes #1651

Closed

Description

@lukehutch

Describe the bug

When a user is already logged in, the session manager listeners are called twice on app startup, both times with the login state being the same (once from _loadSharedPrefs, then once from refreshSession). This breaks the ChangeNotifier convention of only notifying listeners when something actually changes.

This is kind of a pain, because my login system relies on being notified just once when a user is logged in -- so I have to de-dup the change listener calls, using ValueNotifier (which properly follows the ChangeNotifier contract of only notifying when changes are actually made):

final SessionManager sessionManager =
    SessionManager(caller: client.modules.auth)
      ..addListener(() =>
          _sessionManagerSignedInUser.value = sessionManager.signedInUser?.id);

// Need a separate ValueNotifier to de-dup session manager updates
final _sessionManagerSignedInUser = ValueNotifier<int?>(null)
  ..addListener(_sessionManagerSignedInStateListener);

void _sessionManagerSignedInUserListener() async {
  if (_sessionManagerSignedInUser.value != null) {
    // ...
  }
}

Activity

  1. added
    bug
    on Dec 8, 2023
  2. moved this to Bug fixes 🪲 in Serverpod Frameworkson Jun 13, 2024
  3. added
    area: client
    area: authentication
    and removed
    area: client
    on Dec 19, 2024
  4. self-assigned this
    on Jan 27, 2025
  5. tp commented on Jan 27, 2025

    @tp
    Contributor

    @lukehutch We could implement deep-equality on UserInfo in order to avoid the listener being called without any changes.

    I think your mapping into a ValueNotifier (or similarly a ValueListenable<int?> userId) still does make sense though, as the UserInfo object is quite large, and there could be changes to it that are just "minor" updates and not a login/logout: for example when the user object retrieved from the server differs from the one read from local storage.

    In that case (e.g. a name or profile image change) we would still need to notify the outside of that change.

    So I think you approach of focusing only on the userId or a isLoggedIn state is a good one for many apps, to only update specifically when the primary condition changes.

  6. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Jun 27, 2025
  7. tp commented on Jun 27, 2025

    @tp
    Contributor

    The upcoming serverpod_auth_session_flutter package will offer a new SessionManager implementation that uses a ValueNotifier for the "auth info" (user ID + scopes), which will only update on login/logout, when those values actually change.

  8. self-assigned this
    on Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: authenticationbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions