CakePHP:Auth组件未登录 [英] CakePHP: Auth Component not logging in

查看:118
本文介绍了CakePHP:Auth组件未登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习CakePHP,我按照他们给我的每个方向,我得到了很好的结果,但是当涉及到Auth组件(和他们在网站上教的非常简单的认证),我只是可以



方法$ this-> Auth-> login()保持返回false,而不让我登录。



我没有使用任何东西超过教程显示我,我甚至没有哈希的密码(冷静下来,我会,但我需要得到基本的!)。 / p>

这里是我的代码:



模型

  App :: uses('AppModel','Model'); 

class用户扩展AppModel {

public $ validate = array(
'username'=> array(
'notEmpty'=> array (
'rule'=> array('notEmpty'),
'required'=> true
),
),
'password'=> ; array(
'notEmpty'=> array(
'rule'=> array('notEmpty'),
'required'=> true
),
),
);

AppController

  App :: uses('Controller','Controller'); 

类AppController extends Controller {


public $ components = array(

'Session',
' => array(

'loginRedirect'=> array('controller'=>'pages','action'=>'display','home'),
'logoutRedirect'=> array('controller'=>'Users','action'=>'index')



);


}

>

  App :: uses('AppController','Controller'); 

class UsersController extends AppController {

public function login()
{

if($ this-> request-> ('post'))
{
if(!$ this-> Auth-> login())
{
$ this-> Session-& setFlash('Invalid Username或Password');
}
else
{
$ this-> redirect($ this-> Auth-> redirectUrl());
}
}


}

public function logout()
{

$ this-> redirect($ this-> Auth-> logout());

}

View(Users / login.ctp) / strong>

 < div class =login-box> 

<?php echo $ this-> Session-> flash('auth'); ?>

<?php

echo $ this-> Form-> create();

echo $ this-> Form-> input('username');
echo $ this-> Form-> input('password');

echo $ this-> Form-> end('login');

?>

< / div>


解决方案

数据库。当使用AuthComponent :: login()进行认证时,首先对您登录表单中输入的密码进行散列,然后使用数据库中的条目进行检查。由于您在数据库中的条目未散列,所以检查返回false。



在User :: beforeSave()中隐藏


I'm studying right now CakePHP, and I followed every direction they gave me there and I got wonderful results, but when it comes to the Auth Component (and the very simple authentication they teach at the site), I just can't manage to do it.

The method $this->Auth->login() keeps returning false, and not letting me log in.

I am not using anything more than the tutorial shows me, I'm not even hashing the passwords yet (Calm down, I will, but I need to get the basic before!).

Here goes my code:

Model

App::uses('AppModel', 'Model');

class User extends AppModel {

public $validate = array(
    'username' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'required' => true
        ),
    ),
    'password' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'required' => true
        ),
    ),
);

AppController

App::uses('Controller', 'Controller');

class AppController extends Controller {


public $components = array(

    'Session',
    'Auth' => array(

        'loginRedirect'  => array('controller' => 'pages', 'action' => 'display', 'home'),
        'logoutRedirect' => array('controller' => 'Users', 'action' => 'index')

        )

    );


}

Users Controller

App::uses('AppController', 'Controller');

class UsersController extends AppController {

public function login()
{

    if($this->request->is('post'))
    {
        if(!$this->Auth->login())
        {
            $this->Session->setFlash('Invalid Username or Password');
        }
        else
        {
            $this->redirect($this->Auth->redirectUrl());
        }
    }


}

public function logout()
{

    $this->redirect($this->Auth->logout());

}

View (Users/login.ctp)

<div class="login-box">

<?php echo $this->Session->flash('auth'); ?>

<?php

    echo $this->Form->create();

    echo $this->Form->input('username');
    echo $this->Form->input('password');

    echo $this->Form->end('login');

?>

</div>

解决方案

You will have to hash the password before saving it into the database. When authenticating using AuthComponent::login(), the password entered in your login form is first hashed and then checked with the entry in the database. As your entry in the database is not hashed, the check returns false.

Hash the password in User::beforeSave() as mentioned here in the book.

这篇关于CakePHP:Auth组件未登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆