Cookie-Based Login

We have an application which uses Cookie-based Login.

In config/main.php I set the authTimeout under application->components->user to 300 sec

Everything works fine , and indeed , after 300 secs with no user activity , when the logged-in user tries to use the app (e.g click on a button) he gets automatically logged out and redirected to the login view.

My question is : How can I perform some actions after/before automatic logout ?

Which procedure checks if the session is still active or not ?

Can’t figure out how the whole process works :(

Any hints,suggestions would be really appreciated.