I wanted to do something similar to this, but ran into the problem that if they were on another tab taking actions, they could extend the timeout, which would render the original set timeout to no longer be correct.
To account for that, I created a controller in one of my plugins which would get the timeout for the user, and return that.
public function actionGetAuthTimeout()
$authTimeout = craft()->userSession->getAuthTimeout() * 1000;
Then, in the
routes.php file, add a route for that controller action, such as
'getAuthTimeout' => ['action' => 'pluginName/controllerName/getAuthTimeout'],
Then, in the front end, you can access that making sure to pass the param to not extend the session
// Check the remaining timeout until the user is logged out
if (timeout <= 1000)
// I chose to force log them out too, just to make sure
// Do whatever else you want
On a side note, if you want to make this a lot easier for testing, just add the following line in your
craft/config/general.php file to expire after 10 seconds:
'userSessionDuration' => 'PT10S',