narendravaghela / cakephp-remember-me
RememberMe plugin for CakePHP
Installs: 13 564
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 5
Open Issues: 0
Type:cakephp-plugin
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.1.0 <4.0
Requires (Dev)
- cakephp/cakephp-codesniffer: dev-master
- phpunit/phpunit: 4.1.*
This package is auto-updated.
Last update: 2024-10-27 03:10:43 UTC
README
This plugin provides a basic functionality to store user data in Cookies of your CakePHP applications for login and remember user in specific browser.
Read this blog post for detailed example.
Requirements
This plugin has the following requirements:
- CakePHP 3.0.0 or greater.
- PHP 5.4.16 or greater.
Installation
You can install this plugin into your CakePHP application using composer.
composer require narendravaghela/cakephp-remember-me
After installation, Load the plugin
Plugin::load('RememberMe');
Or, you can load the plugin using the shell command
$ bin/cake plugin load RememberMe
Usage
To use this, simply load the RememberMe component from this plugin into your AppController or UsersController.
$this->loadComponent('RememberMe.RememberMe');
You can optionally pass the configuration options for this component.
$this->loadComponent('RememberMe.RememberMe', [ 'cypherKey' => "17485937564892755682047369192734583655920926", // Random unuqie string to encrypt/decrypt data. If not set, default salt value of the application will be used. 'cookieName' => "rememberme", // Name of the cookie. 'period' => '14 Days' // Time period ]);
Here, the basic flow should something like this:
- Find the user from the database
- Validate
- If user has selected "Remember me" checkbox, store the user data using this component.
- Next time, when user (any user) visits the application, check the stored data using
getRememberedData()
. - Use the data returned by
getRememberedData()
and validate against the database and if everything goes well, create a session of the user and make him logged in. - If user manually logs out, simply delete the data from Cookie using
removeRememberedData()
.
Remember data
In your login
action or the action from where user logs into your application, use the rememberData()
function and pass the required data of user being logged in.
$this->RememberMe->rememberData("data@example.com"); // email address of user being logged in // or you can use the array as well $this->RememberMe->rememberData([ 'email' => 'foo@bar.com', 'someUniqueKey' => 'someuniquevalue' ]);
Get remembered data
Generally, in our beforeFilter()
callback, we check whether user is logged in or not. Here, we can use getRememberedData()
to retrieve the data that we have stored in our login action earlier.
If we find something, then we can check it against our user table to check whether there is a user or not.
$isRemembered = $this->RememberMe->getRememberedData(); // code to check this data against database // set the session // ...
Delete data
If you need to remove user data, just call the removeRememberedData()
and it will delete the data from Cookie.
That is it.
Reporting Issues
If you have a problem with RememberMe, please open an issue on GitHub.