auth component wrong redirect cakephp 2.3

4.1k Views Asked by At

my auth component in cake its redirecting wrong. The right path to go should be:

localhost/tlfmovil/usuarios/index

but its redirecting to

http://localhost/tlfmovil/tlfmovil/usuarios

if deactivate the auth component (no login, all access to my site) my site works fine

this is my appcontroller

<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            /*'authorize' => array(
                'Actions' => array('actionPath' => 'controllers')
            ),*/
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Usuario',
                    'fields' => array(
                        'username' => 'email',
                        'password' => 'password'
                    )
                )
            ),
            'authorize' => array('Controller')
        ),

    );

    public function beforeFilter() {
        $this->Auth->loginAction = array('controller' => 'usuarios', 'action' => 'login');
        //$this->Auth->logoutRedirect = array('controller' => 'usuarios', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'usuarios', 'action' => 'add');
        $this->Auth->authError = 'No Posee Permisos para Acceder a esta Sección';
        $this->set('logueado',$this->Auth->loggedIn()); ####Verifica si el usuario esta logueado 
        $this->set('usuarioActual',$this->Auth->user()); #####Manda la informacion del usuario logueado
        //$this->Auth->allow();
    }

    public function isAuthorized($user) {
        if ($user['grupo_id'] == '3'/* Administrador*/ || $user['grupo_id'] =='2' /*Usuario*/){
            return true;
        }

        return false;
    }
}

usuariosController

<?php
class UsuariosController extends AppController{
    var $name = 'Usuarios';
    var $helpers = array('Html','Form');
    //var $scaffold;
...
public function login() {
        if ($this->request->is('post')){     
            if ($this->Auth->login()){
                //Configure::write('Session.timeout','30');
                return $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Usuario o Contraseña Incorrecto'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }


}    

?>

login.ctp

   <?php
    echo $this->Session->flash('auth');
    echo $this->Form->create('Usuario', array('action' => 'login'));
    echo $this->Form->input('email',array('label' => 'Correo Electrónico'));
    echo $this->Form->input('password',array('label' => 'Contraseña'));
    echo $this->Form->end('Ingresar');

?>
1

There are 1 best solutions below

0
nithin On BEST ANSWER

First of all $this->Auth->redirect() is Deprecated in CakePHP 2.3. Since you are using CakePHP 2.3 you should use $this->Auth->redirectUrl() instead.

Secondly redirect()/redirectUrl() will get the URL from where you were redirected to login page. If it is same as login page, then it will use the loginRedirect.

If you always wants to redirect to localhost/tlfmovil/usuarios/index page, use like this.

    public function login() {
        if ($this->request->is('post')){     
            if ($this->Auth->login()){
                $this->redirect(array('controller'=>'usuarios','action'=>'index'));
            } else {
                $this->Session->setFlash(__('Usuario o Contraseña Incorrecto'));
            }
        }
    }