user1402677
user1402677

Reputation: 269

cakephp redirect query

creating a basic log in page - it was working but then we changed our database and our redirect is no longer working.

when we go to log in the site comes back saying that the username/password is incorrect, after checking the sql code that the site is checking against the database - it is sending the correct information but not allowing us to log in.

we want the user when they log into the site to get redirected to the eboxs(controller) home(view).

here is the code in the controller for logging in

    public function login(){

    $this->set('title_for_layout', 'Individual Registration');
    $this->set('stylesheet_used', 'style');
    $this->set('image_used', 'eBOXLogo.jpg');

    if ($this->request->is('post')){
        if ($this->Auth->login()){
            $username = $this->request->data['User']['username'];
            if (0 === $this->User->find('count',array('conditions'=>array('activated'=>true,'username'=> $username)))){
                $this->Session->setFlash('Sorry, your account is not validated yet.');
                $this->redirect($this->referer());
                } 
            else{
                $this->Auth->user('id');
                $this->redirect( array('controller' => 'Eboxs','action' => 'home'));
                }
        }  

        else{
            $this->Session->setFlash('Username or password is incorrect');
        }
    }else{
        $this->Session->setFlash('Welcome, please login');
    }


}

here is the code for the view

<?php    
         echo $this->Form->create('User', array('action' => 'login'));
         echo $this->Form->input('username');
         echo $this->Form->input('password');
         echo $this->Form->end('Login');

     ?> 

Upvotes: 2

Views: 243

Answers (3)

Abid Hussain
Abid Hussain

Reputation: 7762

I think try this

//app controller
class AppController extends Controller {

    public $components = array(
        'Acl',
        'Auth' => array(
            'authorize' => array(
                'Actions' => array('actionPath' => 'controllers')
            )
        ),
        'Session'
    );


    public function beforeFilter() {

     //Configure AuthComponent
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
     $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
     $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
    }

}


//login action view

echo $this->Form->inputs(array(
    'legend' => __('Login'),
    'username',
    'password'
));


//this is controller code


 if ($this->request->is('post')) {
      if ($this->Auth->login()) {
   $this->redirect($this->Auth->redirect());
      } else {
   $this->Session->setFlash('Your username or password was incorrect.');
      }
  }

Upvotes: 1

Arun Jain
Arun Jain

Reputation: 5464

There are following situations that you can check why your code is not working:

  1. Have you used the beforeFilter() method in your User Model?

    If Yes, then check what it is saying.

  2. Put your 'login' method in Auth->allow() method in beforeFilter of your UsersController.

    function beforeFilter(){
        $this->Auth->allow('login');
    }
    
  3. Kindly check whether the associated hashed password saved into your database is equal to the password you are entering for that corresponding user or not. You can check the Auth hashed password using the following syntax:

    pr(AuthComponent::password('USER PASSWORD HERE'));
    
  4. You can use the following syntax to create a form:

    echo $this->Form->create('User', array('url' => $this->request->params));
    

Kindly ask if it still not work for you.

Upvotes: 0

Sitansu
Sitansu

Reputation: 891

Enable the debug mode to '2' in core.php file when you are changing database or you can remove the model cache. You can later on change the debug mode to 0 in the production site. Also check that you have a valid username and password in the users table of the new database.Also check the structure of the password field.It should be varchar 255.

Also Modify the above logic to -

if ($this->Auth->login()){
  $username = $this->request->data['User']['username'];
  if (0 === $this->User->find('count',array('conditions'=>array('activated'=>true,'username'=> $username)))) {

     $this->Session->setFlash('Sorry, your account is not validated yet.');
     $this->redirect($this->referer());
   } else {
    $this->Auth->user('id');
    $this->redirect( array('controller' => 'Eboxs','action' => 'home'));
   }   
} 

In view file instead of using

echo $this->Form->create('User', array('action' => 'login'));

Try using -

echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'login')));

Upvotes: 0

Related Questions