簡體   English   中英

Cakephp Auth登錄無法與其他用戶表一起使用

[英]Cakephp Auth Login not working with a different user table

該功能有問題。 不幸的是,當用戶登錄時,登錄頁面顯示錯誤:“用戶名或密碼無效,請重試”,而不是登錄並重定向到索引頁面。

我的User_Details表具有以下字段:User_Details(用戶ID,用戶名,用戶密碼,電子郵件,已創建,已修改)。

這是我在BlogDetailsController中的登錄函數的代碼:

public function login() {
    $this->loadModel('UserDetail');
    $this->layout = 'login';

    if ($this->request->is('post')) {
        //print_r($this->request->data);
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

這是我的AppController代碼:

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

class AppController extends Controller {

    // added the debug toolkit
    // sessions support
    // authorization for login and logut redirect
    public $components = array(
        'DebugKit.Toolbar',
        'Flash',
        'Session', //Session
        'Auth' => array(//Auth
            'loginAction' => array('controller' => 'BlogDetails', 'action' => 'login'),
            'loginRedirect' => array('controller' => 'BlogDetails', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'BlogDetails', 'action' => 'login'),
            'authError' => "Sorry, you're not allowed to access this page.",
            'Form' => array(
                'userModel' => 'UserDetail',
                'fields' => array('username' => 'user_name', 'password' => 'user_password'))
        )
    );

    // only allow the login controllers only
    public function beforeFilter() {
        $this->Auth->allow('login');
    }

}

這是我的login.ctp代碼:

<?php
echo $this->Flash->render('auth');
echo $this->Form->create('UserDetail', array(
    'inputDefaults' => array(
        'div' => array('class' => 'form-group'),
        'class' => 'form-control',
        'label' => false
    )
));
echo $this->Form->input('user_name', array(
    'before' => '<span class="input-with-icon"><i class="fa fa-user"></i>',
    'after' => '</span>',
    'type' => 'text',
    'title' => 'Please enter username',
    'placeholder' => "User Name",
    'data-rule-requiredchardigitsonly' => true));
echo $this->Form->input('user_password', array(
    'before' => '<span class="input-with-icon"><i class="fa fa-key"></i>',
    'after' => '</span>',
    'type' => 'password',
    'title' => 'Please enter password',
    'placeholder' => "Your Password",
    'data-rule-digit' => true));
$options = array(
    'label' => 'Sign in',
    'div' => array(
        'class' => 'form-group',
    ),
    'class' => 'form-control btn btn-primary',
);
echo $this->Form->end($options);
?>

希望有人可以指出出了什么問題。

我正在使用cakephp版本2.8.1

public function beforeSave($options = array()) {
        // hash our password
        if (isset($this->data[$this->alias]['user_password'])) {
            $this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
        }

        // fallback to our parent
        return parent::beforeSave($options);
    }

已添加到UserDetail模型,並且有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM