TypeConverter
A class that handles the detection and conversion of certain resource formats / content types into other formats.
Table of Contents
Version: 3.3
Requires: PHP 5.2, CakePHP 2.0
Tested On: PHP 5.3, CakePHP 2.0.5
Commit Hash: a9a4c11e30
Released: Feb 1st, 22:28
Requires: PHP 5.2, CakePHP 2.0
Tested On: PHP 5.3, CakePHP 2.0.5
Commit Hash: a9a4c11e30
Released: Feb 1st, 22:28
AutoLogin
Version: 3.3 (logs)
Package: Component: Persistent User Login
Category: CakePHP
Views: 11,740
Permalink -
Tinylink
AutoLogin is a CakePHP Component that will automatically login an Auth session if the user agrees to. The user has the ability to "remember" their login info when logging in and AutoLogin will store their information in a cookie to automatically log them in on their next visit.
Class Features:
Class Features:
- Requires no installation except for adding the checkbox into your user login forms
- Automatically saves the cookie and info when a user logs in
- Automatically kills the cookie and session when a user logs out
- Inserts a hash within the cookie so that it cannot be hijacked
- Encrypts the cookie so the information cannot be harvested
- Configuration options for cookie name and length
- Functionality for additional user updating or error logging
Top
1 - Installation
Once the component has been added to your application, add it to your Controllers $components property.
The AutoLogin component will automatically save the user info to a cookie when they login at the location given for the Auth property $loginAction. It also works when logging out by removing the cookie.
The final step is to create a checkbox in your login form named auto_login. The model used in the form should also match the User model you are using in your Auth.
public $components = array('AutoLogin');
The AutoLogin component will automatically save the user info to a cookie when they login at the location given for the Auth property $loginAction. It also works when logging out by removing the cookie.
The final step is to create a checkbox in your login form named auto_login. The model used in the form should also match the User model you are using in your Auth.
echo $this->Form->input('auto_login', array('type' => 'checkbox', 'label' => 'Log me in automatically?'));
Top
2 - Configuration
If you would like to change the name of the cookie, or the duration until the cookie expires (defaults to 2 weeks), or overwrite the username and password fields, you can change it in your AppController's beforeFilter().
Additionally, AutoLogin will try to determine what controller and action your login takes place at, by using the Auth->loginAction property. If it cannot determine the urls, you can set fallbacks by editing the $settings property (you must supply all settings if you are overwriting).
Additionally, AutoLogin will try to determine what controller and action your login takes place at, by using the Auth->loginAction property. If it cannot determine the urls, you can set fallbacks by editing the $settings property (you must supply all settings if you are overwriting).
public function beforeFilter() { $this->AutoLogin->cookieName = 'rememberMe'; $this->AutoLogin->expires = '+1 month'; $this->AutoLogin->fields = array( 'username' => 'displayName', 'password' => 'passwd' ); $this->AutoLogin->settings = array( 'plugin' => '', 'controller' => 'members', 'loginAction' => 'signin', 'logoutAction' => 'signout' ); }
Top
3 - Login Callbacks
If you need to do additional logging and updating that is not initially in Auths user login (for example updating a users last login time), you can place this extra code in a method called _autoLogin() within your AppController. If Auth login fails, you can do some error logging and reporting by creating a method called _autoLoginError(). Both of these will be called automatically and only if the method exists.
class AppController extends Controller { /** * Run whenever auto login is successful. * * @param array $user - The Auth user session */ public function _autoLogin($user) { } /** * Run whenever auto login fails. * * @param array $cookie - The login cookie data */ public function _autoLoginError($cookie) { } }
Top
Read the whole documentation? Download the script now and try it yourself! Return to the Top
4 - Live Debugging
It can be quite difficult to debug this component if something is not working, simply because this component is automatically ran on each page load and during the login process. You can enable an internal debug system that will email you cookie and user information at specific events so that you may figure out which step the script is dying on. The debug settings take an email (where the debug will be sent) and an array of IPs (emails will only be sent if you are browsing with these IPs). To use the debug, simply set it in bootstrap.php.
Additionally, you can restrict the scope in which the debug emails are delivered. The following scopes are available: login, loginFail, loginCallback, logout, logoutCallback, cookieSet, cookieFail, hashFail, custom (for you to trigger manually).
Configure::write('AutoLogin', array( 'email' => 'email@domain.com', 'ips' => array('127.0.0.1') ));
Additionally, you can restrict the scope in which the debug emails are delivered. The following scopes are available: login, loginFail, loginCallback, logout, logoutCallback, cookieSet, cookieFail, hashFail, custom (for you to trigger manually).
Configure::write('AutoLogin.scope', array('login', 'logout'));