0

In an Angular/Ionic project, where would I use the following attributes to appropriately handle my needs?

document.addEventListener('pause', actiontobeperformed, false);
document.addEventListener('resume', actiontobeperformed , false);

My needs are:

I am building an app that is protected, meaning that you can only view the content when:

you enter the correct access code your session has not timed out But when you go to Home, then I want somehow to record that the session is over and that the user needs to authenticate when he comes back in the app.

WJA
  • 6,676
  • 16
  • 85
  • 152
  • 1
    possible duplicate of [how to check app running in foreground or background in ionic/cordova/phonegap](http://stackoverflow.com/questions/29606012/how-to-check-app-running-in-foreground-or-background-in-ionic-cordova-phonegap) – Sithys Apr 13 '15 at 18:24
  • @sithys: I think I need to rewrite question to: how to use these events in AngularJS? As it is not firing – WJA Apr 13 '15 at 18:37
  • I have reformulated my question. – WJA Apr 14 '15 at 09:57

4 Answers4

3

The cordova pause event might be your answer.

And resume when they return.

ED-209
  • 4,706
  • 2
  • 21
  • 26
1
$(document).ready(function () {
    document.addEventListener('pause', actiontobeperformed, false);
    document.addEventListener('resume', actiontobeperformed , false);
});  

In your js first line or in html inside script tag

Mohammed Imran N
  • 1,279
  • 1
  • 11
  • 26
1

The simplest way to achieve a functionalty like you describe would be like this:

After the User authenticated his Session with his credentials you set a value into the localStorage with localStorage.login=1; for example.

You now add the eventlistener for pause like document.addEventListener('pause', actiontobeperformed, false); and call the function actiontobeperformed after that.

function actiontobeperformed() {
    localStorage.login=0;
}

The only thing you still need is a function which checks the login status. Therefore you could use an if else statement

if (localStorage.login == 1) {
    goto menu;
} else {
    goto loginpage;
} 
Sithys
  • 3,655
  • 8
  • 32
  • 67
1

If you're using Ionic, you should use $ionicPlatform Service, an abstraction of ionic.Platform. read more

$ionicPlatform.on('resume', function(){
      // your staff here
});

$ionicPlatform.on('pause', function(){
      // your staff here
});

So if you want to close session when user press Home button, you should call your AuthService.logout() method inside on Pause code block.

Sergio A.
  • 101
  • 3