CakePHP: Cant do Simple Login

后端 未结 2 2199
刺人心
刺人心 2021-01-17 05:45

i\'m starting to cook cake haha, everytime that i submit my form for to make the authentication, nothing happens!

This\'s my AppController

App::uses         


        
相关标签:
2条回答
  • 2021-01-17 06:31

    I think your problem is here

    public function beforeFilter()
    {       
        $this->Auth->fields = array(
                'username' => 'email',
                'password' => 'senha'
            );
    }
    

    try this

      public function beforeFilter()
        { 
        $this->Auth->authenticate = array(          
                        'Form' => array(
                            'fields' => array(
                                'username' => 'email',
                                'password' => 'senha'
                                    )
                                )
                            )
        }
    

    *@@@ UPDATE @@@@ *

    Well, at last i got the solution...i think so......

    The problem is on your View. Use this-

    <?php echo $this->Form->create('User',array('action'=>'login')); ?>
        <div class="input">
          <div class="blockinput">
            <i class="icon-envelope-alt"></i>
            <input type="mail" name="email" placeholder="Email">
    
            <?php echo $this->Form->input('email', array('type' => 'email', 'placeholder' => 'Email', 'label' => false));?>
    
          </div>
          <div class="blockinput">
            <i class="icon-unlock"></i>
    
           <?php echo $this->Form->input('senha', array('type' => 'password', 'placeholder' => 'Senha', 'label' => false));?>
    
          </div>
        </div>
           <button>Entrar</button>
      <?php echo $this->Form->end(); ?> 
    
    0 讨论(0)
  • 2021-01-17 06:33

    //AppController

    App::uses('AuthComponent', 'Controller/Component');
    
    class AppController extends Controller {
    
        /** 
         * Components
         *
         * @var array
         */
    
        public $components = array(
            'Auth' => array(
                'authenticate' => array(
                    'Form' => array(
                        'fields' => array(
                            'username' => 'email',
                            'password' => 'senha'
                        ),
    
                    )
                ),
                'loginRedirect' => array('controller' => 'users', 'action' => 'home'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'logout'),
                'authError' => 'Por favor, Faça o login para acessar a Área Restrita',
                'loginError' => 'Login ou Senha incorreto!'
                'autoRedirect' => false
        );
    

    view - You have to set Model name User NOT Users in create form

    <?php echo $this->Form->create('User',array('url' => array('controller' => 'users', 'action' => 'login')); 
             echo $this->Form->input('email');
              echo $this->Form->input('senha');
       echo $this->Form->end(); ?> 
    

    //controller

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

    //add this to your User model

    App::uses('AppModel', 'Model');
    /**
     * User Model
     */
    class User extends AppModel {        
    
        public function beforeSave($options = array()) {
    
                    if (!empty($this->data['User']['password'])) {
                        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
                    } 
                    return true;
                }
    }
    

    here is the Simple Auth login

    0 讨论(0)
提交回复
热议问题