Wednesday, April 16, 2014

Re: CakePHP 2.4 Auth->login() always return TRUE ... ?

You are like the 400th person who didnt read that part of the documentation in 2.x
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
(red warning box)

if ($this->Auth->login($this->request->data['User'])) {}
always logs this data in

you need:

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



Am Dienstag, 15. April 2014 13:46:56 UTC+2 schrieb MedAL:
Hello,Iread the documentation to create a simple login form.each time I try to log in it returns true  always;

//UsersController
public function login() { 
if ($this->Auth->user()) {
            $this->redirect($this->Auth->redirect());
        }

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

    $this->Auth->authenticate['Form'] = array('fields' => array('username' => 'username'));
    $this->request->data['User']['id'] = $this->User->id; 
    debug($this->request->data);
       if ($this->Auth->login($this->request->data['User'])) {
        $this->Session->setFlash(sprintf("Welcome %s!", $this->Auth->user('username')));
           return $this->redirect($this->Auth->redirect());
       }
       $this->Session->setFlash(__('Invalid username or password, try again'));
   }
}





//AppController
class AppController extends Controller {

public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'Events',
                'action' => 'index'
            ),
            'loginAction' => array('controller' => 'Users', 'action' => 'login'),

            'logoutRedirect' => array(
                'controller' => 'Users',
                'action' => 'login',
                'home'
            ),'authorize' => array('Controller')

        )
    );

    public function beforeFilter() {
    //parent::beforeFilter();
        $this->Auth->allow('index', 'view','login');
        /* $this->Auth->authenticate = array(
   'Basic' => array('userModel' => 'User'),
   'Form' => array('userModel' => 'User')
);*/

    }



//login.ctp


<div class="users form">
<?php echo $this->Session->flash('auth'); ?>

<?php echo $this->Form->create('User'); ?>

    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('username')
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: