(I'm new to cakePHP 3 so be gentle :-) )
I play with Auth and build the Examples. Now I have two problems.
My first problem: The password hashing don't work (He insert the passwords as plain text). Maybe because I changed the fields. My second problem: How can I check if the mail is already in use (exists in Database) before he save the new record.
Here is my Code
Model/Table/UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('fc_admin_login');
}
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('log_mail', 'Geben Sie eine gültige E-Mail ein')
->notEmpty('log_pw', 'Geben Sie ein Passwort ein')
->add('repeat_password', 'no-misspelling', [
'rule' => ['compareWith', 'admin_login_password_hash'],
'message' => 'Passwords are not equal',]);
}}
Model/Entity/User.php
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity
{
protected $_accessible = [
'*' => true,
'id' => false
];
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}}
Controller/UsersController.php
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post'))
{
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user))
{
$this->Flash->success(__('Inserted'));
return $this->redirect(['action' => 'add']);
}
$this->Flash->error(__('Error'));
}
$this->set('user', $user);
}
Template/Users/add.ctp
<div class="panel-body">
<?= $this->Form->create($user) ?>
<fieldset>
<div class="form-group">
<?= $this->Form->label('log_mail', 'Mail'); ?>
<?= $this->Form->input('log_mail', array('type' => 'email', 'label' => false, 'placeholder' => 'name@provider.com', 'class' => 'form-control')); ?>
</div>
<div class="form-group">
<?= $this->Form->label('log_pw', 'Ihr Passwort'); ?>
<?= $this->Form->input('log_pw', array('type' => 'password', 'label' => false, 'placeholder' => '********', 'class' => 'form-control')); ?>
</div>
<div class="form-group">
<?= $this->Form->label('repeat_password', 'Repeat pw'); ?>
<?= $this->Form->input('repeat_password', array('type' => 'password', 'label' => false, 'placeholder' => '********', 'class' => 'form-control')); ?>
</div>
</fieldset>
<hr/>
<?= $this->Form->button('Submit', ['type' => 'submit', 'class' => 'btn btn-info btn-block']); ?>
<?= $this->Form->end() ?>
</div>