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

Not getting it to work #11

Closed levi956 closed 1 year ago

levi956 commented 1 year ago

I'm trying to apply this plugin to detect idle activity to log users out. Eventually, I used Riverpod per the documentation, but I wasn't getting any event from the stream. Then I copied the example code from the example folder, but still not detecting any event.

I'm testing on an Xcode simulator ios 16.0 and ios physical device.

Is there some sort of configuration I'm missing or something?

SankethBK commented 1 year ago

Hi, Can you post the code

levi956 commented 1 year ago

`class MyApp extends ConsumerWidget { final bool? logged; const MyApp({ super.key, this.logged, });

@override Widget build(BuildContext context, WidgetRef ref) { final sessionController = ref.watch(sessionControllerProvider);

ExpireSession.sessionConfiguations()
    .stream
    .listen((SessionTimeoutState timeoutEvent) {
  sessionController.add(SessionState.startListening);
  if (timeoutEvent == SessionTimeoutState.userInactivityTimeout) {
    pushTo(context, const ForgotPassword());
  } else if (timeoutEvent == SessionTimeoutState.appFocusTimeout) {
    pushTo(context, const ForgotPassword());
  }
});
return SessionTimeoutManager(
  sessionConfig: ExpireSession.sessionConfiguations(),
  sessionStateStream: sessionController.stream,
  child: MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'App',
    theme: GobsThemeData.gobsThemeData,
    home: logged! ? SignIn(isWelcome: logged!) : const Onboard(),
  ),
);

} } `

SankethBK commented 1 year ago

Pass a lower value for userActivityDebounceDuration it's default value is 10 seconds, means it will record one event per 10 seconds by default to prevent spawning of multiple timer objects. If you want to notice events touch immediately set it to a lower value like 1 second

SessionTimeoutManager(
     userActivityDebounceDuration = const Duration(seconds: 1)}
     ...

Let me know if it solves your issue

SankethBK commented 1 year ago

I also suspect if ExpireSession.sessionConfiguations() returns a new SessionConfig object everytime it is called? If that's the case, you can store it in a variable instead of creating 2 different sessionConfigs

final sessionConfig = ExpireSession.sessionConfiguations(); 

   sessionConfig.stream.listen((SessionTimeoutState timeoutEvent) {
  sessionController.add(SessionState.startListening);
  if (timeoutEvent == SessionTimeoutState.userInactivityTimeout) {
    pushTo(context, const ForgotPassword());
  } else if (timeoutEvent == SessionTimeoutState.appFocusTimeout) {
    pushTo(context, const ForgotPassword());
  }
});
return SessionTimeoutManager(
  sessionConfig: sessionConfig,
  sessionStateStream: sessionController.stream,
  child: MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'App',
    theme: GobsThemeData.gobsThemeData,
    home: logged! ? SignIn(isWelcome: logged!) : const Onboard(),
  ),
);
levi956 commented 1 year ago

Still didn't work

SankethBK commented 1 year ago

Can you post the implementation of ExpireSession.sessionConfiguations()

levi956 commented 1 year ago

class ExpireSession { static SessionConfig sessionConfiguations() { return SessionConfig( invalidateSessionForAppLostFocus: const Duration(seconds: 5), invalidateSessionForUserInactivity: const Duration(seconds: 5), ); } }

SankethBK commented 1 year ago

I made some changes in my above code, can you try it. Basically the issue is it returns a new sessionConfig everytime, each sessionConfig is associated with different stream. So make sure to use same sessionConfig object to sessionTimeoutManager and listen to strean

levi956 commented 1 year ago

It still didn't work.

SankethBK commented 1 year ago

Can you create a minimal example to reproduce the problem?

levi956 commented 1 year ago

How can I create a minimum example? I shared my code that includes the problem. I don't think the stream is detecting any event.

SankethBK commented 1 year ago

It would be helpful if you can reproduce the issue in DartPad with minimal code and share the link or share the link to your project if it's not private.

levi956 commented 1 year ago

Would do that shortly.

levi956 commented 1 year ago

https://github.com/levi956/local_session_issue-11

Here is the link to the project. Thank you.

SankethBK commented 1 year ago

When you pass sessionStateStream argument, you have to also call sessionController.add(SessionState.startListening); to start listening. However if you don't pass this optional parameter, it will always listen.

Let me know if this solves your issue.

levi956 commented 1 year ago

It's added in the project. Line 22 on the project. It was always there.

SankethBK commented 1 year ago

Line 22; is not the right place to put it. In that case it will be executed only after the stream emits it's first event which doesn't. So put it outside that callback

levi956 commented 1 year ago

It's working now. Thank you for taking your time to help my fix. I really appreciate.