Disable Auto Redirect after logging into Cakephp 2.4

Asked

Viewed 837 times

8

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

I try to access: /projects/Edit/34 without being logged in. Cakephp then redirects to /login. After informing the user and password and authenticating Cakephp itself redirects me to /projects/Edit/34. Ok, so far so good, but it turns out that when I access the home page of my project and click on the login link (going to /login from the /) and authenticity it redirects me to the previous page, in case the initial of my project.

I would like in this case to be redirected to /panel

How to disable this auto redirect 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 tries to directly access the link /projects/Edit/34 without logging in.
  • Cakephp Auth does not allow accessing and redirecting to /login
  • After user log in 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 on the "Login" menu and goes to the login/login form
  • User logs in and is redirected to the home page /

In this second case, I would not like it to go to the home page but to what is configured in loginRedirect in the case for /panel

  • You can post the code of how you set up the Auth component?

  • Appcontroller.php added

  • 2

    Good that you solved it! Could you edit the question by removing the solution, and posting that part as an answer? It is best suited to the site proposal. And you can/should accept the response itself in this case. Thank you!

  • Oh yes, I was wondering if it was to edit or add new answer. I will edit.

3 answers

5

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

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

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

And in the UsersController.php my method login was 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 matrix $cfg \the/

2

Just do this:

Just summarizing the code below, what you should do is just add this instruction after logging in:

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

This way login works the ways you expect:

  • If the user accesses a page that requires login, was will be redirected to it again after login
  • If the user clicks on login (in the frontend for example), and logs in, it will be redirected to the 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’ve helped

0

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

In the Controller Users:

public function beforeFilter() {
    if($this->action === 'login' && $this->Auth->loggedIn()) {
        $this->redirect(array('controller' => 'users', 'action' => 'painel'));
    }
    parent::beforeFilter();
}
  • Actually I would like for certain actions it does not consider auto redirecting and does not check whether you are logged in or not.

  • I believe it is not possible, just doing the check before to be able to do the redirect you want...

  • I changed it with two cases. I can’t find a way to check the previous page. I believe it stores in Section the page before login.

  • 1

    @Just checking you’re logged in not to redirect if you’re not. And you would have to include this in every controller/action you want to treat as a special case. The redirect destination the cake stores in the session, if I’m not mistaken in the key Auth.redirect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.