在CakePHP v2.0中使用电子邮件地址进行认证 [英] Authenticating with email address in CakePHP v2.0

查看:151
本文介绍了在CakePHP v2.0中使用电子邮件地址进行认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Okies这个问题类似于我最近在Stack Overflow上问过的问题,但是我基本上只是使用CakePHP Book中的代码而不是我自己的代码来尝试理解为什么有些东西不起作用。



基本上,这个想法是允许用户使用他们的电子邮件地址以及他们在2.0版本中的用户名登录。但是它总是返回的细节是不正确的,但我可以STILL用用户名登录,所以基本上,在AppController的覆盖不会改变任何东西...更多,所以我想弄清楚如何允许两个字段登录。



如原始帖子中所述:在CakePHP v2.0中使用电子邮件地址或用户名登录 @nIcO已经将一些可能在两个领域中起作用的东西放在一起,但是这里解释的问题导致它不会工作。



任何想法?任何人都有使用2.0版电子邮件登录。

  // AppController 

public $ components = array
'Auth'=> array(
'loginAction'=> array(
'controller'=>'users',
'action'=> '
),
'authenticate'=> array(
'Form'=> array(
'fields'=&电子邮件)



);

// UsersController

public function login(){
if($ this-> request-> is('post')){
if($ this-> Auth-> login()){
return $ this-> redirect($ this-> Auth-> redirect());
} else {
$ this-> Session-> setFlash(__('Username或password is incorrect'),'default',array(),'auth');
}
}
}


解决方案>

虽然id添加在UsersController(不是在AppController),但这对我工作的电子邮件作为用户名:

 

public $ components = array('Auth');

// BeforeFilter in UsersController
function beforeFilter(){
parent :: beforeFilter();
$ this-> Auth-> loginAction = array('controller'=>'users','action'=>'login');
$ this-> Auth-> authenticate = array(
'Form'=> array(
'fields'=> array('username'=>'email')

);
}

希望它以某种方式起作用


Okies this question is similar to one I have asked recently on Stack Overflow, but I'm basically just using the code from the CakePHP Book rather than my own code to try and understand why something is not working.

Basically the idea is to allow a user to login using their email address as well as their username in version 2.0 of Cake. However it always returns that the details are incorrect but I can STILL login with the username, so basically the override in the AppController does not change anything... More so I'm trying to figure out how to allow both fields for logging in.

As discussed in the original post here: Login with email address or username in CakePHP v2.0 @nIcO has put together something that could pontentially work for both fields BUT the issue explained here causes it not to work.

Any ideas? Anyone got email login working with version 2.0.

// AppController

public $components = array(
    'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login'
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

// UsersController

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

解决方案

Though id added it in UsersController (not in AppController), but this worked for me for email as username:


public $components = array('Auth');

//beforeFilter in UsersController
function beforeFilter() {
   parent::beforeFilter();
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->authenticate = array(
            'Form' => array(                
                'fields' => array('username' => 'email')
            )
        );
}

Hope it helps in some way

这篇关于在CakePHP v2.0中使用电子邮件地址进行认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆