<p>You can end a user's session with the <code>craft()-&gt;userSession-&gt;logout()</code>, method, which the <code>UserSessionService</code> class <a href="http://www.yiiframework.com/doc/api/1.1/CWebUser#logout-detail" rel="nofollow noreferrer">inherits from the <code>CWebUser</code> class</a>.</p>

<p>So, your <code>userLogout</code> function would look like this:</p>

<pre><code>function userLogout(){
    if (craft()-&gt;userSession-&gt;isLoggedIn()){
        craft()-&gt;userSession-&gt;logout(false);
    }
}
</code></pre>

<h2>Additional information</h2>

<p>Passing <code>false</code> to the <code>logout</code> method tells Yii not to "destroy" the session, and mimics the call in Craft's <a href="https://craftcms.com/classreference/controllers/UsersController#actionLogout-detail" rel="nofollow noreferrer"><code>UsersController::logout</code> method</a>.</p>

<p>According to <a href="http://www.yiiframework.com/doc/api/1.1/CHttpSession#destroy-detail" rel="nofollow noreferrer">the Yii documentation</a>, <code>destroy</code> "frees all session variables and destroys all data registered to a session." I'm unclear why Craft chooses not to destroy the session.</p>

<p>The <code>UsersController::logout</code> method also includes a few cleanup tasks, but those seem to be duplicates of the tasks performed in the <a href="https://craftcms.com/classreference/controllers/UsersController#actionLogout-detail" rel="nofollow noreferrer"><code>UserSessionService::afterLogout</code></a> method.</p>

<p>The <code>afterLogout</code> method is called from the <code>craft()-&gt;userSession-&gt;logout()</code> method, so you shouldn't have to worry about this manual cleanup.</p>

CC BY-SA 3.0
2017-02-20T11:05:31.460Z
2017-02-20T11:05:31.460Z
Stephen Lewis
<p>I need a function to logout a craft user and delete the session of that user. This must take place in the <a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow noreferrer">element API plugin</a> as an endpoint. Something like this:</p>

<pre><code>function userLogout(){
    if (craft()-&gt;userSession-&gt;isLoggedIn()){
        // if user loggedin
        // how to logout?
    }
}

'api/logout' =&gt; array(
    'elementType' =&gt; 'User',
    'criteria' =&gt; ['id' =&gt; craft()-&gt;userSession-&gt;user-&gt;id, 'limit' =&gt; 1 ],
    'paginate' =&gt; false,
    'transformer' =&gt; function(\Craft\UserModel $user) {
        userLogout();
    }
),
</code></pre>

<plugin-elementapi><login>
How delete a user session with the element api?
# How delete a user session with the element api?

(Asked by: username_0 on 2017-02-20)

I need a function to logout a craft user and delete the session of that user. This must take place in the element API plugin as an endpoint. Something like this:
function userLogout(){
    if (craft()->userSession->isLoggedIn()){
        // if user loggedin
        // how to logout?
    }
}

'api/logout' => array(
    'elementType' => 'User',
    'criteria' => ['id' => craft()->userSession->user->id, 'limit' => 1 ],
    'paginate' => false,
    'transformer' => function(\Craft\UserModel $user) {
        userLogout();
    }
),

**Tags:** plugin-elementapi, login


---

## 1 answers

### Answer by username_1 on 2017-02-20. Score: 2 | Accepted answer

You can end a user's session with the craft()->userSession->logout(), method, which the UserSessionService class inherits from the CWebUser class.
So, your userLogout function would look like this:
function userLogout(){
    if (craft()->userSession->isLoggedIn()){
        craft()->userSession->logout(false);
    }
}

Additional information
Passing false to the logout method tells Yii not to "destroy" the session, and mimics the call in Craft's UsersController::logout method.
According to the Yii documentation, destroy "frees all session variables and destroys all data registered to a session." I'm unclear why Craft chooses not to destroy the session.
The UsersController::logout method also includes a few cleanup tasks, but those seem to be duplicates of the tasks performed in the UserSessionService::afterLogout method.
The afterLogout method is called from the craft()->userSession->logout() method, so you shouldn't have to worry about this manual cleanup.


---

