简体   繁体   English

cakePHP 2.4中的身份验证

[英]Authentication in cakePHP 2.4

I cannot log in users in cakePHP 2.4, following the documentation I've created this. 我无法在cakePHP 2.4中登录用户,遵循我创建的文档。 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. 它表示已成功登录并重定向到目标URL(用户/已登录操作),但$this->Auth->username$this->Auth->password保持空白,不会创建任何会话。 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 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 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 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>

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. Auth对象中没有字段用户名。

You can't read the password field from Auth using user method. 您无法使用用户方法从Auth读取密码字段。

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: Edit2:通过在Auth中添加以下行来指定身份验证处理程序:

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

Edit3: Check again that the database field storing the password is VARCHAR with 40 length. Edit3:再次检查存储密码的数据库字段是VARCHAR,长度为40。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM