<p>What you want is this:</p>

<pre class="lang-php prettyprint-override"><code>// Returns how many seconds are left in the current user session.
// (int) The seconds left in the session, or -1 if their session
// will expire when their HTTP session ends.
craft()-&gt;userSession-&gt;getAuthTimeout()
</code></pre>

<p>You can then combine this with a Javascript <code>setTimeout</code> when the page loads so you can trigger whatever messages you need.</p>

<p><strong>Edit:</strong> You can use this in your template files:</p>

<pre class="lang-twig prettyprint-override"><code>{% set authTimeout = craft.session.getAuthTimeout() %}
</code></pre>

CC BY-SA 3.0
2016-02-03T09:10:28.577Z
Thanks for your answer, did you get this to work in a template on a non-CP page? Are you sure userSession is available to the front end? I tried calling userSession on index as a test, and just got `Method "userSession" for object "Craft\CraftVariable" does not exist`.
Merlin
CC BY-SA 3.0
2016-02-03T09:43:46.620Z
I edited your post (I hope you don't mind), it was session rather than userSession on the front end! I was however hoping to include all the js to create the logout modals, etc?
Merlin
CC BY-SA 3.0
2016-02-03T11:41:19.977Z
Whoops, was looking at the service name when writing that template snippet! Thanks for the edit though @Merlin :)
Ben Fleming
CC BY-SA 3.0
2016-02-03T14:36:53.323Z
I'll mark this as the answer as it is essentially what I wanted, and the JS, well I'll just have to build that myself. Thank you Benjammin'!
Merlin
CC BY-SA 3.0
2016-02-02T22:41:01.513Z
2016-02-03T11:40:03.503Z
2016-02-03T11:40:03.503Z
Ben Fleming
<p>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.</p>

<p>To account for that, I created a controller in one of my plugins which would get the timeout for the user, and return that.</p>

<pre class="lang-php prettyprint-override"><code>public function actionGetAuthTimeout()
{
    $authTimeout = craft()-&gt;userSession-&gt;getAuthTimeout() * 1000;

    $this-&gt;returnJson($authTimeout);
}
</code></pre>

<p>Then, in the <code>routes.php</code> file, add a route for that controller action, such as</p>

<pre class="lang-php prettyprint-override"><code>'getAuthTimeout' =&gt; ['action' =&gt; 'pluginName/controllerName/getAuthTimeout'],
</code></pre>

<p>Then, in the front end, you can access that <em>making sure to pass the param to not extend the session</em></p>

<pre class="lang-js prettyprint-override"><code>document.addEventListener("DOMContentLoaded", function(event)
{ 
    checkTimeout()
})

// Check the remaining timeout until the user is logged out
function checkTimeout()
{
    $.get('/auth/getAuthTimeout&amp;dontExtendSession=1', function(timeout)
    {
        if (timeout &lt;= 1000)
        {
            // I chose to force log them out too, just to make sure
            window.axios.get('/logout')

            // Do whatever else you want
        }
        else
        {
            setTimeout(function()
            {
                checkTimeout()
            }, timeout)
        }
    })
}
</code></pre>

<p>On a side note, if you want to make this a lot easier for testing, just add the following line in your <code>craft/config/general.php</code> file to expire after 10 seconds:
<code>'userSessionDuration' =&gt; 'PT10S',</code></p>

CC BY-SA 4.0
2019-03-28T16:46:03.837Z
2019-04-04T19:09:06.733Z
2019-04-04T19:09:06.733Z
Paul Le Grand
<p>I love the session handling on the backend CP. The prompts that come up on session expiration after setting <code>'userSessionDuration' =&gt; 'PT5M',</code> in <code>config/general.php</code> are exactly what I want to use on the frontend website, (shown below). </p>

<p><a href="https://i.sstatic.net/KXY0C.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KXY0C.png" alt="CP Password Prompt"></a></p>

<p>I would like to use this logic on the frontend to force session-ends after x minutes. Doing some digging I can see the logic lies in <code>UserController</code> under <code>actionGetAuthTimeout()</code>, and this is polled while I'm on the CP every minute. 
My question is whether I would have to manually run the controller constantly in the background and make the modal pop up? is there a more elegant method I've missed?</p>

<p>Thanks!</p>

<users><frontend><session><current-user><timeout>
Using the CP login session expiration for the frontend
# Using the CP login session expiration for the frontend

(Asked by: username_0 on 2016-02-02)

I love the session handling on the backend CP. The prompts that come up on session expiration after setting `'userSessionDuration' => 'PT5M',` in `config/general.php` are exactly what I want to use on the frontend website, (shown below).

[![CP Password Prompt](https://i.sstatic.net/KXY0C.png)](https://i.sstatic.net/KXY0C.png)

I would like to use this logic on the frontend to force session-ends after x minutes. Doing some digging I can see the logic lies in `UserController` under `actionGetAuthTimeout()`, and this is polled while I'm on the CP every minute.
My question is whether I would have to manually run the controller constantly in the background and make the modal pop up? is there a more elegant method I've missed?

Thanks!

**Tags:** users, frontend, session, current-user, timeout


---

## 2 answers

### Answer by username_1 on 2016-02-02. Score: 3 | Accepted answer

What you want is this:
// Returns how many seconds are left in the current user session.
// (int) The seconds left in the session, or -1 if their session
// will expire when their HTTP session ends.
craft()->userSession->getAuthTimeout()

You can then combine this with a Javascript setTimeout when the page loads so you can trigger whatever messages you need.
Edit: You can use this in your template files:
{% set authTimeout = craft.session.getAuthTimeout() %}

> **Comments:**
> **username_0**: Thanks for your answer, did you get this to work in a template on a non-CP page? Are you sure userSession is available to the front end? I tried calling userSession on index as a test, and just got `Method "userSession" for object "Craft\CraftVariable" does not exist`.  
> **username_0**: I edited your post (I hope you don't mind), it was session rather than userSession on the front end! I was however hoping to include all the js to create the logout modals, etc?  
> **username_1**: Whoops, was looking at the service name when writing that template snippet! Thanks for the edit though @username_0 :)  
> **username_0**: I'll mark this as the answer as it is essentially what I wanted, and the JS, well I'll just have to build that myself. Thank you Benjammin'!  

---

### Answer by username_2 on 2019-03-28. Score: 1

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;

    $this->returnJson($authTimeout);
}

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
document.addEventListener("DOMContentLoaded", function(event)
{ 
    checkTimeout()
})

// Check the remaining timeout until the user is logged out
function checkTimeout()
{
    $.get('/auth/getAuthTimeout&dontExtendSession=1', function(timeout)
    {
        if (timeout <= 1000)
        {
            // I chose to force log them out too, just to make sure
            window.axios.get('/logout')

            // Do whatever else you want
        }
        else
        {
            setTimeout(function()
            {
                checkTimeout()
            }, timeout)
        }
    })
}

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',


---

