简体   繁体   中英

cakephp Auth login not working

I have table user his Model is

class User extends AppModel {
    public $useTable = 'user';
public function beforeSave($options = array()) {    
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);}
return true;
}

in my Userscontroller

public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());

            } else {
                $this->Session->setFlash(__("Nom d'user ou mot de passe invalide, réessayer"));

            }
        }
    }

In my view login.ctp I have

<?php
echo $this->Session->flash(); ?>
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
    <fieldset>
        <legend><?php echo __('Merci de rentrer votre nom d\'utilisateur et mot de passe'); ?></legend>
        <?php echo $this->Form->input('email');
        echo $this->Form->input('password');

    ?>
        <?php echo $this->Form->end(__('Connexion'));?>
            <a style="color: #616060;font-size: 15px;font-weight: bold;float: right;" href="<?php
                echo $this->Html->url(array('controller'=>'users','action'=>'forgotten')); ?>" >
                Mot de passe oublié</a>
    </fieldset>
    <p style="font-size:22px;"><b>N.B :</b> Veuillez Vous inscrire d’abord au niveau de Myblan application mobile !!</p>
</div>
 <?php echo $this->element('sql_dump'); ?>

My table user

CREATE TABLE `user` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `id_mobile` VARCHAR(225) NOT NULL DEFAULT '',
    `password` VARCHAR(45) NULL DEFAULT NULL,
    `nom_complet` VARCHAR(225) NULL DEFAULT NULL,
    `sexe` VARCHAR(225) NULL DEFAULT NULL,
    `age` VARCHAR(225) NULL DEFAULT NULL,
    `adresse` VARCHAR(225) NULL DEFAULT NULL,
    `tel` VARCHAR(45) NULL DEFAULT NULL,
    `email` VARCHAR(225) NULL DEFAULT NULL,
    `created` DATETIME NULL DEFAULT NULL,
    `point` INT(11) NULL DEFAULT '0',
    `role_user` VARCHAR(45) NULL DEFAULT NULL,
    `code_password` VARCHAR(30) NULL DEFAULT NULL,
    `active` INT(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`, `id_mobile`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;

in AppController

class AppController extends Controller {
     public $components = array('Session',
    'Auth' => array(
        'authorize' => array('Controller'),
         'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email'),
                'password' => 'password',
                'scope' => array('User.active' => 1)
            )
        ),
        'loginRedirect' => array('controller' => 'users', 'action' => 'index')

     )
 );

   public function beforeFilter() {
       parent::beforeFilter();
   }

public function isAuthorized($user) {
    return true;
}

}

but when I want to connect with my login and password nothing is displayed or the error code or redirect me to my account

in your model

public function beforeSave($options = array()) {
        // hash our password
        if (!$this->id) {
            $passwordHasher = new SimplePasswordHasher();
            $this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
        }

    return true;
}

in your appcontroller

    public $components = array('Session',
    'Auth' => array(
        'authorize' => array('Controller'),
         'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email',
                'password' => 'password'),
                'scope' => array('User.active' => 1)
            )
        ),
        'loginRedirect' => array('controller' => 'users', 'action' => 'index')

     )
 );
public function beforeFilter() {
    Security::setHash('sha1');

    $this->Auth->allow('login','add', 'index');
}

in your data base password just VARCHAR(30)

in your UserController goes this:

public function beforeFilter() {
    parent::beforeFilter();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM