Disable Auto Redirect after login in CakePHP 2.4

8

I'm using CakePHP 2.4 Auth and if I try to access a link that needs to login it redirects to the login form. For example:

I try to access: / projects / edit / 34 without being logged in. Then CakePHP redirects to / login. After informing the user and password and authenticating CakePHP itself it redirects me to / projects / edit / 34. Okay, so that's alright, but it happens that when I access the homepage of my project and click on the login link (going to / login from the /) and authentic it redirects me to the previous page, in the case the initial one of my project.

I would like this case to be redirected to / panel

How to disable this auto redirection in CakePHP only for specific actions?

Follow my AppController.php

class AppController extends Controller {
 public $components = array(
'DebugKit.Toolbar',
'Session', 
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'User', 
                'fields' => array('username' => 'usuario', 'password' => 'senha'), 
                'scope' => array('User.status' => 1)
            )
        ), 
        'authorize' => 'Controller', 
        'loginAction' => array('controller' => 'users', 'action' => 'login'), 
        'loginRedirect' => array('controller' => 'users', 'action' => 'painel'), 
        'logoutRedirect' => array('controller' => 'home', 'action' => 'index'), 
        'authError' => 'Você não tem permissão para acessar.' 
    )
);

 public function isAuthorized($user){
    return true;
}

public $helpers = array('Html', 'Form', 'Session');

}

Two cases:

First:

  • User attempts to directly access the / projects / edit / 34 link without logging in.
  • CakePHP Auth does not allow access and redirects to / login
  • After login Auth redirects to / projects / edit / 34

In the first case it is ok, understood and working as it should. Now in the second case:

  • User enters the homepage of the site /
  • User clicks the "Login" menu and goes to the login form in / login
  • User logs in and is redirected to the home page /

In this second case, I would not want it to go to the home page but rather to what is set up in loginRedirect in the case for / panel

    
asked by anonymous 08.01.2014 / 19:19

3 answers

5

With the tips I did the following and what I wanted was solved:

In AppController.php I added an array with the links that I want to disable auto redirect.

public $cfg = array(
    'disabledAuthAutoRedirect' => array('/')
);

And in UsersController.php my method login looks like this:

public function login(){

  if ($this->Auth->loggedIn()){
    return $this->redirect($this->Auth->loginRedirect);
  }

  if ($this->request->is('post')){

    if ($this->Auth->login()){

      if ($this->Session->check('Auth.redirect')){

        if (in_array($this->Session->read('Auth.redirect'), $this->cfg['disabledAuthAutoRedirect'])){
          return $this->redirect($this->Auth->loginRedirect);
        }
      }

      return $this->redirect($this->Auth->redirect());
    }

    $this->Session->setFlash('Usuário ou senha inválidos, tente novamente.');

    unset($this->request->data['User']['senha']);
  }
}

Now just add more items in the array $cfg \ o /

    
08.01.2014 / 21:01
2

Just do this:

Just summarizing the code below, what you should do is just add this statement after login:

$this->redirect($this->Auth->redirect());

This way login works the way you expect it to:

  • If the user accesses a page that needs login, it will be redirected to it again after login
  • If the user clicks login (in the frontend for example), and logs in, they will be redirected to loginRedirect

Tested in version 2.4.3 Stable

Login

/**
 * login method
 *
 * @param 
 * @return void
 */    
public function login(){

  $this->layout = 'login';

  if($this->request->is('post')) {
    if($this->Auth->login()) {
      $this->Session->setFlash(__('Login efetuado com sucesso!'), 'flash/admin/success');
      $this->redirect($this->Auth->redirect());
    } else {
      $this->Session->setFlash(__('Usuário e/ou senha incorretos'), 'flash/admin/error');
      $this->redirect($this->Auth->redirect());
    }
  }

}

Logout

/**
 * logout method
 *
 * @param 
 * @return void
 */
public function logout() {
  $this->Session->setFlash(__('Logout efeutado com sucesso!'), 'flash/admin/success');
  $this->redirect($this->Auth->logout());
}

AppController.php

'Auth' => array(
  'authenticate' => array(
    'Form' => array(
      'fields' => array('username' => 'email')
    )
  ),
  'loginAction' => array('controller' => 'users', 'action' => 'login', 'admin' => false),
  'logoutAction' => array('controller' => 'users', 'action' => 'logout', 'admin' => false),
  'loginRedirect' => '/admin',
  'logoutRedirect' => array('controller' => 'users', 'action' => 'login', 'admin' => false),
  'authError' => 'Acesso não permitido.'
),

I hope I have helped

    
11.01.2014 / 14:27
0

I'm not sure, but I think Cake is behaving in the expected way - from his point of view. You can try to intercept action access on the controller itself and force a redirect.

No Controller Users:

public function beforeFilter() {
    if($this->action === 'login' && $this->Auth->loggedIn()) {
        $this->redirect(array('controller' => 'users', 'action' => 'painel'));
    }
    parent::beforeFilter();
}
    
08.01.2014 / 20:05