为什么$ this-> Auth-> login()总是返回false? (CakePHP 2.x)

时间:2015-12-05 08:37:04

标签: cakephp authentication cakephp-2.6 cakephp-2.x

我正在使用cakephp 2.6.7。

内部App控制器:

   public $components = array(
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'fields' => array(
                        'username' => 'email', //Default is 'username' in the userModel
                        'password' => 'password'  //Default is 'password' in the userModel
                    ),
                    'passwordHasher' => array(
                        'className' => 'Simple',
                        'hashType' => 'sha256'
                    )
                )
            )
        )
    );

Inside ResellersController:

   public $components = array(
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'Reseller',
                    )
                ),
            'loginAction' => array(
                'controller' => 'resellers',
                'action' => 'login'
                ),
            'loginRedirect' => array('controller' => 'resellers', 'action' => 'profile'),
            'logoutRedirect' => array('controller' => 'resellers', 'action' => 'login'),
            'authError' => "You can't   acces that page",
            'authorize' => 'Controller'
            )
        );

  public function isAuthorized($user = null) {
        return true;
    }

function login() {
    $this->layout = 'public-login';
    $this->loadModel('Reseller');
        // if already logged in check this step
    if ($this->Auth->loggedIn()) {
            return $this->redirect('profile'); //(array('action' => 'deshboard'));
        }
        // after submit login form check this step
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $msg = '<div class="alert alert-error">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>Incorrect email/password combination. Try Again</strong>
            </div>';        
            $this->set(compact('msg'));
        }
    }
}

Inside Resellers / login.ctp:

  <?php
            echo $this->Form->create('Reseller', array(
                'inputDefaults' => array(
                    'label' => false,
                    'div' => false
                ),
                'class' => 'login-form',
                'url' => array('controller' => 'resellers', 'action' => 'login')
                    )
            );
            ?>

            <?php if(isset($msg)){
             echo $msg;
            } 
            ?>

            <h3 class="form-title">Login to your account</h3>
            <div class="alert alert-danger display-hide">
                <button class="close" data-close="alert"></button>
                <span>
                    Enter Email and password. </span>
            </div>
            <div class="form-group">
                <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
                <label class="control-label visible-ie8 visible-ie9">Email</label>
                <div class="input-icon">
                    <i class="fa fa-user"></i>

                    <?php
                    echo $this->Form->input(
                            'email', array(
                        'class' => 'form-control placeholder-no-fix',
                        'type' => 'text',
                        'autocomplete' => 'off',
                        'placeholder' => 'Email'
                            )
                    );
                    ?>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label visible-ie8 visible-ie9">Password</label>
                <div class="input-icon">
                    <i class="fa fa-lock"></i>

                    <?php
                    echo $this->Form->input(
                            'password', array(
                        'class' => 'form-control placeholder-no-fix',
                        'type' => 'password',
                        'autocomplete' => 'off',
                        'placeholder' => 'Password'
                            )
                    );
                    ?>
                </div>
            </div>

            <div class="form-actions">
                <?php
                echo $this->Form->button(
                        'Login <i class="m-icon-swapright m-icon-white"></i>', array(
                    'class' => 'btn blue pull-right',
                    'type' => 'submit',
                    'escape' => false
                        )
                );
                ?> 

            </div>


            <div class="forget-password">
                <h4>Forgot your password ?</h4>
                <p>
                    no worries, click <a href="javascript:;" id="forget-password">
                        here </a>
                    to reset your password.
                </p>
            </div>
            <div class="create-account">
                <p>
                    Don&#39;t have an account yet ?&nbsp; <a style=" text-transform: none; font-size: 12px !important;" class="btn btn-circle blue" href="javascript:;" id="register-btn">
                        Create an account </a>
                </p>
            </div>
            <?php echo $this->Form->end(); ?>
            <!-- END LOGIN FORM -->

Inside Model / Reseller.php:

<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class Reseller extends AppModel {

    var $name = "reseller";

    //  public $belongsTo = array(
    //     'Order' => array(
    //         'className' => 'Order',
    //         'foreignKey' => 'api_key'
    //     )
    // );

    public $validate = array(
        'email' => array(
            'rule' => 'isUnique',
            'required' => true,
            'message' => 'Email already exist'
        ),
        'password' => array(
            'rule' => array('minLength', '4'),
            'message' => 'password must be minimum 4 characters long'
        )
    );

    function hashPassword() {
     if (!empty($this->data[$this->alias]['password'])) {
            $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }
    }

    function beforeSave($options = array()) {
        $this->hashPassword();
        return true;
    }

}

?>

我一直在探索我的代码两天。但是不要找出为什么它总是返回错误。 有许多问题与我的相同,我研究每一个但没有运气。任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

您正在覆盖AuthComponent属性中的ResellersController:$components并且未定义身份验证字段。

$components中的ResellersController定义应为:

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'Reseller',
                'fields' => array(
                    'username' => 'email', 
                    'password' => 'password'  
                ),
                'passwordHasher' => array(
                    'className' => 'Simple',
                    'hashType' => 'sha256'
                ),
            )
        ),
        'loginAction' => array(
            'controller' => 'resellers',
            'action' => 'login'
        ),
        'loginRedirect' => array('controller' => 'resellers', 'action' => 'profile'),
        'logoutRedirect' => array('controller' => 'resellers', 'action' => 'login'),
        'authError' => "You can't acces that page",
        'authorize' => 'Controller'
    )
);   

除非您希望自己的转销商只使用ResellersController中提供的操作,否则您应该将身份验证与AppController的身份验证合并。

我注意到你没有闪现Auth错误。请尝试在您的视图中包含以下内容:

<?php echo $this->Session->flash('auth'); ?>
相关问题