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

timeoutEvent == SessionTimeoutState.userInactivityTimeout #13

Closed blacknight9 closed 1 year ago

blacknight9 commented 1 year ago

hey,

i installed your package and in my test, it looks like this function keep triggering when i'm active no the way around !

  if (timeoutEvent == SessionTimeoutState.userInactivityTimeout) {
        // handle user  inactive timeout
        // Navigator.of(context).pushNamed("/auth");

      }
SankethBK commented 1 year ago

It will be triggered due to user inactivity, can you post the values you are passing for SessionConfig eg:

final sessionConfig = SessionConfig(
    invalidateSessionForAppLostFocus: const Duration(seconds: 15),
    invalidateSessionForUserInactivity: const Duration(seconds: 30));
blacknight9 commented 1 year ago

I copied your example to my. Project, and i used snackbar for test, and it's only triggered when there is activity, when i stop nothing happens, i can share a video if you want

@override
  Widget build(BuildContext context) {
    final sessionConfig = SessionConfig(
        invalidateSessionForAppLostFocus: const Duration(seconds: 15),
        invalidateSessionForUserInactivity: const Duration(seconds: 5));

    sessionConfig.stream.listen((SessionTimeoutState timeoutEvent) {
      if (timeoutEvent == SessionTimeoutState.userInactivityTimeout) {
        log(timeoutEvent.toString(),name: 'timeoutEvent == SessionTimeoutState.userInactivityTimeout');
        // handle user  inactive timeout
        // Navigator.of(context).pushNamed("/auth");

        Get.snackbar('userInactivityTimeout', 'userInactivityTimeout');
      } else if (timeoutEvent == SessionTimeoutState.appFocusTimeout) {
        Get.to(()=>ProfilePageScreen());
        // handle user  app lost focus timeout
        // Navigator.of(context).pushNamed("/auth");
        Get.snackbar('appFocusTimeout', 'appFocusTimeout');
      }
    });

    return SessionTimeoutManager(
      sessionConfig: sessionConfig,
      child: GetMaterialApp().. 
SankethBK commented 1 year ago

Can you increase invalidateSessionForUserInactivity to a higher value, say 30 seconds.

i can share a video if you want

Yes that would be helpful

blacknight9 commented 1 year ago

Can you increase invalidateSessionForUserInactivity to a higher value, say 30 seconds.

i can share a video if you want

Yes that would be helpful

the 5 sec is just for test, this shouldn't effect how the functionality works ,

here is the video, notice the beginning and the end is where no activity was going and nothing triggered until i start moving around (still set to 5 seconds)

https://youtu.be/YqNxlrSDZ4Y give the video a minute to finish processing on Ytb

SankethBK commented 1 year ago

Got it, it appears that timer is starting only after the first touch right?

blacknight9 commented 1 year ago

Yes, and then it's suspended. And again triggered after activity.

But that's not it, technically it should not be triggerd since user is active.

Thank you for you prompt reply

SankethBK commented 1 year ago

But that's not it, technically it should not be triggerd since user is active.

Yes that shouldn't be happening, can you post minimum code required to reproduce this bug

SankethBK commented 1 year ago

I got it. Default value of userActivityDebounceDuration is 10 seconds which means it records only one user event for 10 seconds (to prevent memory leaks because of setTimeout objects), you can change this value to a lower number like 1 or 2 seconds. You can pass it as a parameter to SessionTimeoutManager

SankethBK commented 1 year ago

Regarding the timer starting only after the initial touch event, let me know if its an issue for your use-case

blacknight9 commented 1 year ago

But that's not it, technically it should not be triggerd since user is active.

Yes that shouldn't be happening, can you post minimum code required to reproduce this bug

check this https://github.com/blacknight9/local_session_timeout_test

blacknight9 commented 1 year ago

Regarding the timer starting only after the initial touch event, let me know if its an issue for your use-case

yes ,, the issues i'm facing:

1- counter doesn't start until initial touch 2- the function for no activity is triggered when user is active

SankethBK commented 1 year ago

Did you try this, this should solve second point

I got it. Default value of userActivityDebounceDuration is 10 seconds which means it records only one user event for 10 seconds (to prevent memory leaks because of setTimeout objects), you can change this value to a lower number like 1 or 2 seconds. You can pass it as a parameter to SessionTimeoutManager

SankethBK commented 1 year ago

For 1st point,SessionTimeoutManager takes an optional parameter sessionStateStream, you can pass SessionState.startListening event to that stream as soon as your app renders (you can refer example app for SessionState usage) sessionStateStream.add(SessionState.startListening);

blacknight9 commented 1 year ago

how to use this ?

because when i add this argument to SessionTimeoutManager i get error

type 'SessionState' is not a subtype of type 'Stream<SessionState>?'

blacknight9 commented 1 year ago

Did you try this, this should solve second point

I got it. Default value of userActivityDebounceDuration is 10 seconds which means it records only one user event for 10 seconds (to prevent memory leaks because of setTimeout objects), you can change this value to a lower number like 1 or 2 seconds. You can pass it as a parameter to SessionTimeoutManager

yes, thank you, this works

SankethBK commented 1 year ago

because when i add this argument to SessionTimeoutManager i get error

The parameter itself is a stream, you need to pass SessionState.startListening to that stream. Refer the example app, let me know if you need any help