dongyaobo9081 2015-10-09 12:27
浏览 69
已采纳

Cakephp:使用AuthComponent登录Admin

I have one table whose name is admins. Its contains username and password fields. My sales user use admins table for the sales login. I want to login sales using AuthComponent. I have write code for this as below.

AppController

public $components = [ 
        'Auth' => [ 
                'loginAction' => [ 
                        'controller' => '',
                        'action' => 'login' 
                ],
                'logoutRedirect' => [ 
                        'controller' => '',
                        'action' => 'login' 
                ],
                'loginRedirect' => [ 
                        'controller' => '',
                        'action' => 'deshboard' 
                ],
                'className' => 'MyAuth'
        ]]

public function beforeFilter() {
        $this->Auth->authenticate = [
                    'Form' => ['userModel' => 'admin', "fields" => ["username" => "username", 
                                                                                "password" => "password"]
                    ]];
}

SalesController

function login() {
    $post = $this->request->data('Admin');

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

        //var_dump($this->Auth->login());exit;
        if ($this->Auth->login()) {
            return $this->Auth->redirect($this->Auth->redirectUrl());
        }

        // perform login throttling (failure and block) if Sales or Admin portal
        // set an appropriate failure message
    }
}

When I have print the return value of auth->login() function. Its always return false.

I have search a lots for this issue but I am unable to find any proper answer.

Thanks in advance for helping me.

  • 写回答

1条回答 默认 最新

  • doukuang6795 2015-10-16 11:23
    关注

    I have find the solution for my above issue.

    This issue is because of passwordHaser. I use difference password in the add or update password so we have to define the passwordHaser in the component configuration, if we don’t use simple encription.

     'Auth' => [
            'className'      => 'MyAuth',
            'authenticate'   => [
                'Form' => [
                    'userModel' => 'Admin',
                    'fields'    => [
                        'username' => 'username',
                        'password' => 'password'
                    ],
                    'passwordHasher' => [
                        'className' => 'Simple'
                    ]
                ]
            ],
        ],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办