How can I create an automatic logout with ionic 4?

I want to create a system in my ionic application that after a time of about two hours the user is disconnected, and can only be connected the next day. Can anybody help me?

You could add some details in ionic storage for that. However, it’s better to maintain these on the server if user would clear app data on android or ios just in case.

1 Like

so I am storing the data in the ionic store because the application will stay most of the offiline time, I am using the setInterval to call the method every 5 seconds inside the app.component.ts, but it is returning “ERROR Error: Uncaught (on promise): TypeError: Unable to read the ‘getUsuarios’ property of undefined.”

inside the constructor:

    let interval = setInterval(this.logoutTimeout,1000);

the function I’m calling:

  async logoutTimeout(){
      let horas = new Date();
      let hora = horas.toLocaleTimeString(); 
      console.log("Hora atual: ",hora);
      let dataAgora = new Date();
      await this.storageService.getUsuarios().then(user => {
        if(user[0].tempoFinal.getHours() > dataAgora.getHours() && user[0].tempoFinal.getDay() === dataAgora.getDay()){
        console.log("tempofinal: ",user[0].tempoFinal.toLocaleTimeString());

the dataAgora would be the current date, and tempoFinal would be the time that the User has until it is disconnected

Can you find the error?

Can’t be very sure unless I can see full code. However, looking through the error saying Unable to read the ‘getUsuarios’ property of undefined , it looks like storageService is undefined since this is passed as a callback function. So probably the context of this is lost. You could use arrow functions to overcome this if this was not accessible. So try and change your code as below and see if it works:

 async logoutTimeout(){


logoutTimeout = async () => {
//your code
  • However, I think we can adopt a better approach.
  • Instead of setInterval, whenever you are making a call to storageService methods, just check if current access time is 2 hours past the first access, if yes, disconnect the user, else continue.
  • To achieve this, you can make every method of storageService pass through a single method which is defined in the storageService itself.
  • This would be efficient as well compared to setInterval which needs to check after every 5 seconds.
1 Like

but how would I do to check all the time if the user is passing the time limit by the method you said to be more efficient?

Didn’t get you. You mean the check inside the method would consume some time?

1 Like

In case, how is the operation of the second solution you proposed? How would you find out if the user would be within the usage time or not? I think I did not understand the other solution without using setInterval.

Ok, it would go something like below:

let’ say you have a value stored in ionic storage such as first_access_time. Then further code goes into storageService as below:

checkIfTimeElapsed = async () => {
	let first_access_time = await'first_access_time').then((first_access_time) => {
		return first_access_time;
	}).catch(err => {
		console.log('could not fetch access time');

	if(new Date().getTime() - first_access_time >= 7200){ // 7200 seconds in 2 hours
		// your logout action;

	// else take no action 

getUsuarios = async () => {
	await this.checkIfTimeElapsed();
	// your further code