Rob Haisfield
Rob Haisfield

Reputation: 1

Looking to automatically time out users on Firebase but having some difficulty

I've got an iPhone app where you create an account, and while you're logged in, you can be either active or inactive. To be clear, even when you're inactive, you're still logged in. I want to make it so that if a user is active but hasn't checked the app or used it for 45 minutes, they are automatically made inactive. Is that possible? If so, how would I go about it? I'm not trying to force a log out, but rather change their state from active to inactive. so in firebase, for each user, it has a variable for isActive. I'm trying to make it so that turns from 1 to 0 after a certain amount of idle time.

Thank you!

Upvotes: 0

Views: 741

Answers (2)

Abhishek Sharma
Abhishek Sharma

Reputation: 29

Make a timer variable and make it work. Attach a mousemove event to reset the timer not stopping the timer. If the time elapsed is greater than your desired limit call the signOut method in firebase.

why mousemove? To notice the inactivity.

Actually I am a web developer most of the time dealing with javascript. I don't know which programming language you are using so use similar method as setTimeOut in javascript.

Upvotes: 0

Jay
Jay

Reputation: 35657

This can be done by leveraging a Cron Job.

There are two things that will need to happen.

You'll need determine what mechanics indicate a user is active or not. For example, every time the users taps a Search button, that timestamp is written to Firebase. As long as the difference between that time and the current time is less than 45 minutes, take no action.

A cron job to trigger code* that compares a current time stamp to the written time stamp and if greater than 45 minutes, set's the user to inactive. (actually just querying for all users where current time - posted time > 45 minutes, and set them all to inactive)

*code could be integrated with Firebase Cloud Functions, or your app could be observing a node that's set as well.

The app could also trigger this sequence but if it goes offline the active/inactive node would not be set or cleared. However, there is an onDisconnect Firebase function you may want to look into as well

We have a little app we've used to automatically log an employee out of an account if they go afk, and this design pattern works well.

Upvotes: 1

Related Questions