douzi9211 2014-05-19 11:59
浏览 5
已采纳

cakePHP 2.4中的身份验证

I cannot log in users in cakePHP 2.4, following the documentation I've created this. It says successfully logged in and also redirects to the destination URL (the users/loggedin action) but $this->Auth->username and $this->Auth->password stays blank, no sessions get created. It also gets redirected when trying to log in with wrong credentials. And also tells me the login was successful. I'm clueless now.

AppController.php

class AppController extends Controller {
  public $components = array(
        'Session',
        'DebugKit.Toolbar',
        'Auth' => array(
            'loginAction' => array('controller' => 'users', 'action' => 'login', 'plugin' => false),
            'loginRedirect' => array('controller' => 'users', 'action' => 'loggedin'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'loggedout'),
            'authError' => 'UNABLE TO LOG IN.'
        )
    );

    public function beforeFilter(){
        $this->Auth->userModel = 'User';
        $this->Auth->fields = array('username' => 'username', 'password' => 'password');
        $this->Auth->allow('index','display','login','show_reg_form', 'view','signup');
    }

}

UsersController.php

I'll only paste the login function:

    public function login() {


    if ($this->request->is('post')) {

            if($this->Auth->login(/*$this->request->data*/)){
                $this->Session->setFlash(__('Successfully logged in.')); 
                return $this->redirect($this->Auth->redirectUrl()); 
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }

    }

login.ctp

The relevant part is:

<div class="container-login users form">
                        <?php echo $this->Form->create('User');
                             echo $this->Form->input('username');
                             echo $this->Form->input('password');
                            echo $this->Form->end(__('Login',true));?>

                    </div>
  • 写回答

1条回答 默认 最新

  • dongzhi1822 2014-05-19 12:13
    关注

    The logged in user details should be read like this:

    $id          = $this->Auth->user('id');
    $username    = $this->Auth->user('username');
    ...
    $other_field = $this->Auth->user('other_field');
    

    The is no field username in the Auth Object.

    You can't read the password field from Auth using user method.

    You will need to query the user from the database if you want to get the password. But you should leave the password hashed and alone.

    Edit: Since you can access the 'loggedin' page this means that there is a user session created. You just need to change the way you access the user data.

    Edit2: Specify the authentication handlers by adding following line to your Auth:

    'Auth' => array(
        ....
        'authenticate' => array('Form') 
    )
    

    Edit3: Check again that the database field storing the password is VARCHAR with 40 length.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向