3

I am trying to implement the auto logout feature after x mins of inactivity on flutter while using Firebase , authentication method being email.

I have searched online but whatever I've found is not for flutter.

Any help will be greatly appreciated thank you!

Jojo888
  • 39
  • 3

2 Answers2

0

you can use interceptor for all api instance like this, but instead customize the onRequest method.

the idea is: save time information when hit api occurred. and then whenever another hit api occur, check duration between now and last saved time.

if the duration is longer than, let's say 5 minutes, then you can call method logout, else you can continue the request

here some snippet to make it clear:

Future<Dio> getApiClient() async {
_dio.interceptors.clear();
_dio.interceptors
    .add(InterceptorsWrapper(onRequest: (RequestOptions options) {
  // Do something before request is sent

  var pref = await SharedPreferences.getInstance();
  var timeNow = DateTime.now().millisecondsSinceEpoch;
  var lastHitApi = pref.getInt(LAST_HIT_API);
  var delay = timeNow - lastHitApi;
  pref.setInt(LAST_HIT_API, timeNow);

  if (delay > DELAY_MAX) {
    // do logout here
  }


  return options;

},onResponse:(Response response) {
    // Do something with response data

    return response; // continue

}, onError: (DioError error) async {
  // Do something with response error

}));

_dio.options.baseUrl = baseUrl;
return _dio;

}

Edit: i guess this one is more preferable

nashihu
  • 750
  • 7
  • 17
-1

Set the timeout duration and call logout funtion

Timer(Duration(seconds: 5), () => logOut());