dtvam48220 2014-03-19 16:43
浏览 40
已采纳

如何使用Auth

I am trying to use Auth in CakePhp. I followed some other post but none of them seem to work. Here's what I have:

AppController.php

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'BlowFish', 
                'fields' => array('email' => 'email', 'password' => 'password')
            )
        ),
        'loginRedirect' => array('controller' => 'poll', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'public', 'action' => 'index'), 
        'flash' => array(
            'element' => 'alert',
            'key' => 'auth',
            'params' => array(
                'plugin' => 'BoostCake',
                'class' => 'alert-error'
            )
        )
    )
);

User.php (model)

App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel{
    public function beforeSave($option = array()){
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']);
        return true;
    }
}

UserController.php

public function login(){
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Session->setFlash('Incorrect email and password');
        }
    }
}

Login.php

<h1>Login</h1>

<?php 
    echo $this->Form->create();
    echo $this->Form->input('email');
    echo $this->Form->input('password');
    echo $this->Form->end('Authenticate')
?>
  • 写回答

1条回答 默认 最新

  • duanhan8757 2014-03-19 17:00
    关注

    Found the error. Even though it the input field on the form is "email", in Auth, it is consider "username"

    so

        echo $this->Form->input('email');
        echo $this->Form->input('password');
    

    translates to this

        fields' => array('username' => 'email', 'password' => 'password')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度