Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to change the cookie name #11

Merged
merged 2 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 63 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ Run : `composer require xety/cake3-cookieauth:1.*`
Or add it in your `composer.json`:
``` php
"require": {
"xety/cake3-cookieauth": "1.*"
"xety/cake3-cookieauth": "1.*"
},
```

## Configuration
``` php
'Xety/Cake3CookieAuth.Cookie' => [
'cookie' => [
'name' => 'CookieAuth'
]
]
```
All others configuration option can be found on the official [CakePHP documentation](http://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers).

## Usage
In your `config/bootstrap.php` add :
``` php
Expand All @@ -29,69 +39,69 @@ Plugin::load('Xety/Cake3CookieAuth');
In your `AppController` :
``` php
public $components = [
'Cookie',
'Auth' => [
'authenticate' => [
'Form',
'Xety/Cake3CookieAuth.Cookie'
]
]
'Cookie',
'Auth' => [
'authenticate' => [
'Form',
'Xety/Cake3CookieAuth.Cookie'
]
]

];
```

In your `AppController`, in the `beforeFilter` action :
``` php
public function beforeFilter(Event $event) {
//Automaticaly Login.
if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {

$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
} else {
$this->Cookie->delete('CookieAuth');
}
}
//Automaticaly Login.
if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {

$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
} else {
$this->Cookie->delete('CookieAuth');
}
}
}

//If you want to update some fields, like the last_login_date, or last_login_ip, just do :
public function beforeFilter(Event $event) {
//Automaticaly Login.
if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
$this->loadModel('Users');

$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);

$user = $this->Users->newEntity($user);
$user->isNew(false);
//Last login date
$user->last_login = new Time();
//Last login IP
$user->last_login_ip = $this->request->clientIp();
//etc...

$this->Users->save($user);
} else {
$this->Cookie->delete('CookieAuth');
}
}
//Automaticaly Login.
if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
$this->loadModel('Users');

$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);

$user = $this->Users->newEntity($user);
$user->isNew(false);

//Last login date
$user->last_login = new Time();
//Last login IP
$user->last_login_ip = $this->request->clientIp();
//etc...

$this->Users->save($user);
} else {
$this->Cookie->delete('CookieAuth');
}
}
}
```

In your `login` action, after `$this->Auth->setUser($user);` :
``` php
//It will write Cookie without RememberMe checkbox
$this->Cookie->configKey('CookieAuth', [
'expires' => '+1 year',
'httpOnly' => true
'expires' => '+1 year',
'httpOnly' => true
]);
$this->Cookie->write('CookieAuth', [
'username' => $this->request->data('username'),
'password' => $this->request->data('password')
'username' => $this->request->data('username'),
'password' => $this->request->data('password')
]);


Expand All @@ -101,14 +111,14 @@ echo $this->Form->checkbox('remember_me');

//In the login action :
if($this->request->data('remember_me')) {
$this->Cookie->configKey('CookieAuth', [
'expires' => '+1 year',
'httpOnly' => true
]);
$this->Cookie->write('CookieAuth', [
'username' => $this->request->data('username'),
'password' => $this->request->data('password')
]);
$this->Cookie->configKey('CookieAuth', [
'expires' => '+1 year',
'httpOnly' => true
]);
$this->Cookie->write('CookieAuth', [
'username' => $this->request->data('username'),
'password' => $this->request->data('password')
]);
}
```

Expand Down
9 changes: 7 additions & 2 deletions src/Auth/CookieAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class CookieAuthenticate extends BaseAuthenticate
public function __construct(ComponentRegistry $registry, array $config = [])
{
$this->_registry = $registry;
$this->config([
'cookie' => [
'name' => 'CookieAuth'
]
]);
$this->config($config);
}

Expand All @@ -39,7 +44,7 @@ public function authenticate(Request $request, Response $response)
throw new \RuntimeException('You need to load the CookieComponent.');
}

$cookies = $this->_registry->Cookie->read('CookieAuth');
$cookies = $this->_registry->Cookie->read($this->_config['cookie']['name']);
if (empty($cookies)) {
return false;
}
Expand Down Expand Up @@ -79,6 +84,6 @@ public function implementedEvents()
*/
public function logout(Event $event, array $user)
{
$this->_registry->Cookie->delete('CookieAuth');
$this->_registry->Cookie->delete($this->_config['cookie']['name']);
}
}