CakePHP3: how to log in as another user
<p>As administrator, I'd like to take another identity to access the backoffice as a normal user.</p>

<p>So I select a user in a list and submit it to my controller.</p>

<p>I tried the following code but I never access to target url, I receive HTTP code 302 as If I tried to access the url, without any user logged in.</p>

<p>What's the problem?</p>

<pre><code>    $id = $this-&gt;Auth-&gt;user('id');
    $user = $this-&gt;Users-&gt;get($this-&gt;Auth-&gt;user('id'));

    if (($user['role'] == 'admin') &amp;&amp; (!empty($this-&gt;request-&gt;data))) {

        $id = $this-&gt;request-&gt;data['id'];

        if (!$this-&gt;Users-&gt;exists($id)) {
            throw new NotFoundException(__('Invalid User'));
        }

        $user = $this-&gt;Users-&gt;findById($id);
        $this-&gt;Auth-&gt;setUser($user-&gt;toArray());

        return $this-&gt;redirect([
            'controller' =&gt; 'Owners', 
            'action' =&gt; 'account', 
            'prefix' =&gt; 'pros'
        ]);

    }
</code></pre>