SankethBK / local_session_timeout

Redirect user to authentication page if the application doesn't receive any user interaction, or been running in the background for "x" duration.
https://pub.dev/packages/local_session_timeout
BSD 3-Clause "New" or "Revised" License
12 stars 19 forks source link

Irregular Session State Stream behaviour #21

Closed DavidOrakpo closed 11 months ago

DavidOrakpo commented 1 year ago

I have my SessionStateStream set up in a Provider using River Pod like so:

final inactivityManagerProvider = ChangeNotifierProvider(
  (ref) => InactivityManagerViewModel(),
);

class InactivityManagerViewModel with ChangeNotifier {
  var sessionStateStream = StreamController<SessionState>();

  SessionConfig? sessionConfig;
}

In MyApp's build method, I have initialized it like this:

final timeOutProvider = ref.watch(inactivityManagerProvider);
    timeOutProvider.sessionConfig = SessionConfig(
      invalidateSessionForAppLostFocus: const Duration(seconds: 40),
      invalidateSessionForUserInactivity: const Duration(seconds: 40),
    );
    timeOutProvider.sessionConfig?.stream.listen((timeOutEvent) async {
      // timeOutProvider.sessionStateStream.add(SessionState.stopListening);
      if (timeOutEvent == SessionTimeoutState.userInactivityTimeout) {
        await ref
            .read(authenticationProvider)
            .signOut(authToken: getIt<UserDependencies>().getAuthToken())
            .then((value) {
          NotificationManager.topBarNotifyError("Signed Out Due to Inactivity");
        });
      } else if (timeOutEvent == SessionTimeoutState.appFocusTimeout) {
        await ref
            .read(authenticationProvider)
            .signOut(authToken: getIt<UserDependencies>().getAuthToken())
            .then((value) {
          NotificationManager.topBarNotifyError("Signed Out Due to Inactivity");
        });
      }
    });
But whenever I want to use the sessionStateStream.add(SessionState.startListenint) from the provider, it pops the screen I'm going to and I'm not sure why.
SankethBK commented 1 year ago

Hi @DavidOrakpo , can you attach small demo if possible