Sunday, March 16, 2014

Can't login with CakePHP's Auth login (Invalid password or username)

Hi guys.

I'm using cakephp 2.x and can't seem to make Auth->login() work.

app/Controller/AppController.php

class AppController extends Controller {
   
       // sitema de autentificacao simples http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
  
public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array('controller'=>'eleitores', 'action'=>'login'),
            'loginRedirect' => array('controller'=>'linhas', 'action'=>'index'),
            'logoutRedirect' => array('controller'=>'pages', 'action'=>'display', 'index'),
            'authorize' => array('Controller'),
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Eleitore',
                    'scope' => array('Eleitore.id' => 1)
                )
            ),
            'authError' => 'Você não possui autorização para acessar esta página!'
        )
    );
 
    public function beforeFilter() {
        $this->Auth->allow();  
    }

...
app/Controller/EleitoresController.php

<?php
App::uses('AppController', 'Controller');
/**
 * Eleitores Controller
 *
 * @property Eleitore $Eleitore
 * @property PaginatorComponent $Paginator
 */
class EleitoresController extends AppController {

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');
       
       
/**
 * index method
 *
 * @return void
 *
 */
    public function beforeFilter() {
        parent::beforeFilter();
         // Allow users to register and logout.
         
        }
       
        public function login() {
             $this->Auth->userModel = 'GroupUser';
            if ($this->request->is('post')) {
                 if ($this->Auth->login()) {
                    return $this->redirect($this->Auth->redirect());
                     }
                 $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }

app/View/Eleitores/login.ctp

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Eleitore'); ?>
    <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: