CakePHP plugin that performs Remember-me authentication with the new cookie algorithm of version 3.5 or later.
- CakePHP 3.5 or later
- PHP 5.6 or later
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require node-link/cakephp-remember-me
Please load the plugin manually as follows:
<?php
// In src/Application.php. Requires at least 3.6.0
use Cake\Http\BaseApplication;
class Application extends BaseApplication
{
public function bootstrap()
{
parent::bootstrap();
// Load the plugin
$this->addPlugin('NodeLink/RememberMe');
}
}
Prior to 3.6.0, you should use Plugin::load()
:
<?php
// In config/bootstrap.php
use Cake\Core\Plugin;
Plugin::load('NodeLink/RememberMe', ['bootstrap' => true]);
Or, use bin/cake
to load the plugin as follows:
bin/cake plugin load -b NodeLink/RememberMe
In the AppController.php
of your application, set up AuthComponent
.
<?php
namespace App\Controller;
use Cake\Controller\Controller;
class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'userModel' => 'Users',
'fields' => ['username' => 'username', 'password' => 'password'],
],
'NodeLink/RememberMe.Cookie' => [
'userModel' => 'Users', // Please set the same as 'Form'.
'fields' => ['token' => 'remember_token'], // Specify the column where you want to save the token for Remember-me authentication.
],
],
]);
// ...
}
}
Please update database and models as necessary.
ALTER TABLE `users` ADD `remember_token` VARCHAR(64) NULL DEFAULT NULL;
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class User extends Entity
{
protected $_hidden = [
'password',
'remember_token', // Add
];
}
Add the following to your login template:
<?= $this->Form->control('remember_me', ['type' => 'checkbox', 'label' => __('Remember me')]); ?>
Or
<?= $this->Form->checkbox('remember_me'); ?>
<?= $this->Form->label('remember_me', __('Remember me')); ?>
Edit config/.env
or config/app.php
.
The full default configuration is as follows:
<?php
return [
'Security' => [
'cookieKey' => env('SECURITY_COOKIE_KEY', env('SECURITY_SALT', '__SALT__')),
],
'RememberMe' => [
'field' => 'remember_me',
'cookie' => [
'name' => 'remember_me',
'expires' => '+1 year',
'path' => '',
'domain' => '',
'secure' => false,
'httpOnly' => true,
],
],
];
It is recommended to set random string to SECURITY_COOKIE_KEY
of config/.env
, or 'Security.cookieKey'
of config/app.php
.
If you have a problem with the RememberMe plugin, please send a pull request or open an issue on GitHub.
Also, I would appreciate it if you contribute to updating README.md
file.