php - CakePhp 3.X Login with POST + AUTH fail -
i'm trying login post , auth unsuccessfully returning false function "$ this-> auth-> identify ()" within userscontroller
here settings appcontroller view
appcontroller
public function initialize() { parent::initialize(); $this->loadcomponent('flash'); $this->loadcomponent('auth'); $this->loadcomponent('cookie'); } function beforefilter(event $event) { $this->auth->allow('add'); $this->auth->config('authorize','controller',true); $this->auth->config('authenticate', ['form' => ['fields' => ['username' => 'username', 'password' => 'password']]]); $this->auth->config('logoutredirect', 'homes/index',true); $this->auth->config('loginredirect', 'homes/index',true); $this->auth->config('loginaction', 'users/login',true); $this->auth->config('loginerror', 'usuário e/ou senha incorreto(s)',true); $this->auth->config('autherror', 'você precisa fazer login para acessar esta página',true); $this->auth->config('usermodel', 'users',true); } userscontroller
public function login() { if ($this->request->is('post')) { /* here return false */ if ($this->auth->identify()) { $this->session->setflash(__('welcome, ' . $this->auth->user('username'))); } else { $this->auth->flash('login errado'); } } } login.ctp
<?= $this->form->create(); ?> <?php // $this->form->create('users'); ?> <fieldset> <legend><?= __('login') ?></legend> <?php echo $this->form->input('username'); echo $this->form->input('password'); ?> </fieldset> <?= $this->form->button(__('login')) ?> <?= $this->form->end() ?> table mysql username , password varchar 200
two things - 1, depending on how strict database is, might have make sure username saved typed it. i've had issue before created username camelcase johndoe. typing 'johndoe' worked me until upgraded mysql enterprise, incredibly picky , demanded type johndoe instead.
2 - looks input names match fields in database, double check you're saving password correctly when set account. i'm going assume followed cakephp 3 tutorial , password being saved , hashed through entity:
protected function _setpassword($password) { return (new defaultpasswordhasher)->hash($password); } // inside tutorial on cake site following instructions in cake tutorial should work you, , sure follow suit $this->auth->setuser($user); afterwards once returns true.
Comments
Post a Comment