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.

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

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号