title: Cakephp3 redirects back to login page immediately after successfully logging in user
body: <p>I have a small application in cake php3 which has a front end and and an admin backend. I am trying to secure the controllers in the admin backend to require authentication before a user proceeds but after following the tutorial on cakephp3 <a href="http://book.cakephp.org/3.0/en/controllers/components/authentication.html" rel="nofollow">http://book.cakephp.org/3.0/en/controllers/components/authentication.html</a>, I discover the $this->Auth->user() returns null and the users is redirected back to the login page immediately.</p>
  
  <p>my login function in /src/Controller/Admin/UsersController.php</p>
  
  <pre><code>public function login() {
       if ($this-&gt;request-&gt;is ( 'post' )) {
          $user = $this-&gt;Auth-&gt;identify ();
          if ($user) {
                  die(var_dump($user));// returns the user details correctly
                  $this-&gt;Auth-&gt;setUser ( $user );             
                  return $this-&gt;redirect($this-&gt;Auth-&gt;redirectUrl()); 
          }            
          $this-&gt;Flash-&gt;error ( __ ( 'Invalid username or password, try again' ) );
  
       }          
  }
  
  
  
  public function beforeFilter(Event $event) {
      parent::beforeFilter ( $event );
      $this-&gt;Auth-&gt;allow ( [  'logout'    ] );
  }
  </code></pre>
  
  <p>And in my route i have </p>
  
  <pre><code> Router::prefix ( 'admin', function ($routes) {
  $routes-&gt;connect ( '/', [ 
          'controller' =&gt; 'Admin',
          'action' =&gt; 'dashboard' 
  ]);
  $routes-&gt;connect ( '/pages/*', [
          'controller' =&gt; 'Admin',
          'action' =&gt; 'dashboard'
  ] );
  
  $routes-&gt;fallbacks ( 'InflectedRoute' );
  </code></pre>
  
  <p>} ); </p>
  
  <p>In my APPController, I have this </p>
  
  <pre><code>public function initialize() {
      $this-&gt;loadComponent ( 'Flash' );
      // $this-&gt;loadComponent('Auth');
      $this-&gt;loadComponent ( 'Auth', [ 
              'authorize' =&gt; ['Controller'], // Added this line
              'loginRedirect' =&gt; [ 
                      'controller' =&gt; 'Admin',
                      'action' =&gt; 'dashboard' 
              ],
              'logoutRedirect' =&gt; [ 
                      'controller' =&gt; 'Users',
                      'action' =&gt; 'login' 
              ],
              'authenticate'=&gt; [
              'Form' =&gt; [
                      'fields' =&gt; [
                              'username' =&gt; 'email',
                              'password' =&gt; 'password'
                      ]
                      ]
                  ]
      ] );
  
  
  }
  public function beforeFilter(Event $event) {
  
      if ((isset ( $this-&gt;request-&gt;prefix ) &amp;&amp; ($this-&gt;request-&gt;prefix === 'admin'))) {
          $this-&gt;layout = 'admin';
      }
  
  
      //exit(var_dump($this-&gt;Auth-&gt;user()));
  
      if ($this-&gt;request-&gt;prefix !== 'admin') {
          $this-&gt;Auth-&gt;allow (); //allow all other parts of the site to work
      }
  
  
  
  }
  
  public function isAuthorized($user) {
  
      if (isset ( $user ['role'] ) &amp;&amp; $user ['role'] === 'admin') {
          return true;
      }
  
      // Default deny
      return false;
  }
  </code></pre>
  
  <p>In my /src/COntroller/Admin/AdminController.php, i have this;</p>
  
  <pre><code>    public function beforeFilter(Event $event) {
      parent::beforeFilter ( $event );
  
  }
  
  public function index() {
  
      return $this -&gt; render('TestView/index');
  
  }
  
  public function dashboard(){
  
  }
  </code></pre>
  
  <p>Any help i get wold be appreciated. I have been cracking and searching the internet for couple of days now. I am new to cakephp but i believe i must have done something wrong. I can provide more insights to the code if required. Thanks in advance.</p>