- CakePHP 1.3 Application Development Cookbook
- Mariano Iglesias
- 487字
- 2021-04-09 22:04:14
By default the Auth
component will use the given username posted in the login form to check for a valid user account. However, some applications have two separate fields: one to define the username, and another one to define the user's e-mail. This recipe shows how to allow logins using either a username or an e-mail.
We should have a fully working authentication system, so follow the entire recipe, Setting up a basic authentication system.
We also need the field to hold the user's e-mail address. Add a field named email
to your users
table with the following SQL statement:
ALTER TABLE `users` ADD COLUMN `email` VARCHAR(255) NOT NULL;
We need to modify the signup page so users can specify their e-mail address. Edit your app/views/users/add.ctp
file and make the following changes:
<?php
echo $this->Form->create();
echo $this->Form->inputs(array(
'legend' => 'Signup',
'email',
'username',
'password'
));
echo $this->Form->end('Submit');
?>
- Edit your
app/views/users/login.ctp
file and make the following changes to it:<?php echo $this->Form->create(array('action'=>'login')); echo $this->Form->inputs(array( 'legend' => 'Login', 'username' => array('label'=>'Username / Email'), 'password' )); echo $this->Form->end('Login'); ?>
- Edit your
UsersController
class and make sure thelogin
action looks like the following:public function login() { if ( !empty($this->data) && !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)) { if ($this->Auth->autoRedirect) { $this->redirect($this->Auth->redirect()); } } else { $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth'); } } }
If you now browse to
http://localhost/users/login
and you can enter the user's e-mail and password to log in, as shown in the following screenshot:
When the Auth
component is unable to find a valid user account using the username and password fields, it gives the control back to the login
action. Therefore, in the login
action we can check if there is any submitted data. If that is the case, we know that the Auth
component was not able to find a valid account.
With this in mind, we can try to find a user account with an e-mail that matches the given username. If there is one, we log the user in and redirect the browser to the default action, similar to what the component would do on a successful attempt.
If we cannot find a valid user account, we simply set the flash message to the default error message specified in the Auth
component.
You may have noticed that when looking for the user record, we used $this->Auth->data
rather than $this->data
to use the actual posted values. The reason for this is because the Auth
component will not only automatically hash the password field, but also remove its value from the controller's data
property, so if you need to show the login form again, the password field will not be pre-filled for the user.
- ANSYS 14熱力學(xué)·電磁學(xué)·耦合場(chǎng)分析自學(xué)手冊(cè)
- Photoshop CS6 商業(yè)應(yīng)用案例實(shí)戰(zhàn)
- Midjourney AI繪畫(huà)藝術(shù)創(chuàng)作教程:關(guān)鍵詞設(shè)置、藝術(shù)家與風(fēng)格應(yīng)用175例
- ERP沙盤(pán)模擬簡(jiǎn)明教程
- 3ds Max & Unreal Engine 4:VR三維建模技術(shù)實(shí)例教程(附VR模型)
- 中文版CorelDRAW X6基礎(chǔ)培訓(xùn)教程
- Photoshop CC從入門到精通(全彩超值版)
- 通達(dá)信炒股軟件從入門到精通(第2版)
- 3ds Max 2015中文版從入門到精通
- WordPress Theme Design
- Premiere Pro影視后期編輯:短視頻制作實(shí)戰(zhàn)寶典
- Oracle Enterprise Manager Grid Control 11g R1: Business Service Management
- Object/Oriented JavaScript
- Cacti 0.8 Network Monitoring
- 蝶變:移動(dòng)用戶體驗(yàn)設(shè)計(jì)之道