2
votes

I have followed the tutorials on the official site but The redirect part is not working for the first time

Step 1 : go to users/login and login with correct credentials.
Result : Login successful but redirected to pages/home instead of articles/index.
Step 2 : again go to users/login and login with correct credentials.
Result : Redirecting properly to articles/index (session is logged in)

How may I fix this?

It seems that it was working fine before I tried the authorisation part.(not sure). But I have commented all authorisation part. Still not fixed.

Anything I am missing?

AppController

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

class AppController extends Controller
{

    public function initialize()
    {
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
        //  'authorize' => ['Controller'], // Added this line
            'loginRedirect' => [
                'controller' => 'Articles',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display',
                'home'
            ]
        ]);
    }

    //public function isAuthorized($user)
    //{
    //    // Site Admin can access every action
    //    if (isset($user['role']) && $user['role'] === 'admin') {
    //        return true;
    //    }
    //
    //    // Default deny
    //       return false;
    //
    //}

    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['index','display']);
    }

}
?>

UPDATE

If I directly access the login URL i.e. users/login and then login, It works perfectly. However I have a button in my pages/home page which redirects to the users/login page on click. By using this route via the button, If I login, I am facing the issue.

Note: If viewed the session information with the debugKit, It shows redirect to /

button code in home.ctp file

<?php echo $this->Html->link('<span class = "glyphicon glyphicon-log-in"></span> Login',['controller' => 'users', 'action' => 'login'],['escape' => false]);?>

Any ideas?

2
Try after clear all cache and session data? - AddWeb Solution Pvt Ltd
@AddWebSolutionPvtLtd I tried it. However I discovered something else. Please find my update in the question - Varun Rao

2 Answers

1
votes

Solve this issue by writing this code:

UsersController:

if($this->Auth->Login()){
    $this->redirect($this->Auth->redirect());
}

AppController:

public function beforeFilter(){
    if($this->here != '/cmap/users/login'){
        $this->Session->write('Auth.redirect', $this->here);
    } 
}

Here, $this->here is your file path. Hope this help you well!

0
votes

From CakePHP documentation, about $loginRedirect: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$loginRedirect

This value will be ignored if the user has an Auth.redirect value in their session.

So your $this->Auth->loginRedirect is being ignored. You can manually delete the Auth.redirect value from the session. This is a quick hack and no need to change the value for every URL.

$session = $this->request->session();
if($session->read('Auth.redirect') === '/') {
    $session->delete('Auth.redirect');
}