简体   繁体   English

在CakePHP的Auth组件中使用用户名而不是电子邮件

[英]Using username instead of email in CakePHP's Auth Component

使用CakePHP的Auth组件,如何允许用户使用“用户名”或“电子邮件”字段作为用户名,并使用“密码”字段作为密码进行身份验证?

what does "using (username and email) both as username " mean? “使用(用户名和电子邮件)都是用户名”是什么意思?

Edit: ok, so you want Auth to look in both username and email fields in the db to compare to the "username" that the user enters? 编辑:好的,您希望Auth查看数据库中的用户名和电子邮件字段,以与用户输入的“用户名”进行比较? then do this: 然后这样做:

 function beforeFilter() { parent::beforeFilter(); $this->Auth->fields = array('username' => 'username', 'password' => 'pass'); $this->Auth->autoRedirect = false; } function login(){ if ($this->Auth->user()) { $this->redirect($this->Auth->redirect()); } else if (!empty($this->data)) { $this->Auth->fields = array('username' => 'email', 'password' => 'pass'); $this->data['User']['email'] = $this->data['User']['username']; if($this->Auth->login($this->data))$this->redirect($this->Auth->redirect()); } } 

To do this you have to skip Auths autoredirect and manage it yourself. 要执行此操作,您必须跳过Auths autoredirect并自行管理。 This the login action in your users_controller: 这是users_controller中的登录操作:

public function login() {
    if(!empty($this->data)) { // Submitted form

        // Try to login with Email
        if(!$this->Auth->user() // if user wasn't logged in with username + pass
            && !empty($this->Auth->data['User']['username'])
            && !empty($this->Auth->data['User']['password'])
        ) {
            $user = $this->User->find('first', array(
                'conditions' => array(
                    'User.email' => $this->Auth->data['User']['username'],
                    'User.password' => $this->Auth->data['User']['password']
                ),
                'recursive' => -1
            ));

            if(!empty($user) && $this->Auth->login($user)) {
                // They logged in, so kill the flash error message
                $this->Session->delete('Message.auth');
            } else {
                $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth');
            }
        }

        if($this->Auth->user()) {
            // Post login logic here
            $this->redirect($this->Auth->redirect());
        }

    } else {
        if($this->Auth->user()) {
            $this->Session->setFlash(__d('users', 'You are already registered and logged in!', true));
            //$this->redirect('/');
            $this->redirect($this->Auth->redirect());
        }
    }

This was copied straight from my app, so may need a bit of tweaking for yours. 这是直接从我的应用程序复制,所以可能需要对你的一些调整。 Don't forget to set $this->Auth->autoRedirect = false; 别忘了设置$this->Auth->autoRedirect = false; in your AppController:beforeFilter(); 在你的AppController中:beforeFilter();

You have to remember that Auth will automatically check against username and password, so this action just picks up from that. 你必须记住,Auth会自动检查用户名和密码,所以这个动作就是从中获取的。 The Session::remove() call is to delete the Auth error message automatically left when the username/password check fails ANd the email login succeeds (otherwise you get error messages with successful logins). Session::remove()调用是删除用户名/密码检查失败时自动离开的Auth错误消息和电子邮件登录成功(否则您会收到成功登录的错误消息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM