2
votes

I am having trouble getting the Auth component redirect.

I have a login form and I want to keep the user on the page he logs in on. For example, if he is viewing contact us page, I want to keep him there after logging in.I have made loginaction page as my login page to validation purpose.Otherwise i can't validate for password blank field. Here is my user's controller code :

function beforeFilter(){

    $this->Auth->allow('register');
         $this->Auth->autoRedirect = false;
    parent::beforeFilter();

}

function loginaction(){

     if($this->data){
    if($this->data['User']['email']=='')
           $this->Session->setflash('E-Mail cannot be blank');
    elseif ($this->data['User']['password']=='')
        $this->Session->setflash('Password cannot be blank');
    else {
                 $this->data['User']['password']=$this->Auth->password($this->data['User']['ppassword']);
         if($this->User->validateUser($this->data)){
         $this->Auth->login($this->data);
         $this->redirect($this->Auth->redirect()); 

} }

and following is my app_controller code:

function beforeFilter(){

        $this->Auth->fields = array(
        'username' => 'email',
        'password' => 'password'
        );

        $this->Auth->loginAction=array('controller'=>'users', 'action'=>'loginaction');


     }

This will redirecting to the page from where user logged out.But i want redirection should happen to the page from where user clicks the login button. I am not getting where i am wrong.

1
Try removing everything from your loginaction action. The Auth Component is supposed to take care of that automatically. - bfavaretto
@bfavaretto: Thanks, I worte loginaction action to customize the error message.If i keep just login, it will show invalid email-id or password, but i want to display error message for blank email-id and blank password. - pushpa
I see that, but your error messages look so simple (... is blank), I thought you could use a single message, something like $this->Auth->loginError = "Email and password cannot be blank"; - bfavaretto
If i do so it will show same error message for invalid username and password. - pushpa

1 Answers

3
votes

If you need to redirect to the referer page you can use:

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