Skip to content

Commit d29115b

Browse files
committed
Access the controller's Auth component instead of reloading it
1 parent 3793f0b commit d29115b

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Installation:
2424
**Then:**
2525

2626
- Add `Plugin::load('AutoLogin');` to your application's `bootstrap.php` (unless if you're already using `Plugin::loadAll();`)
27-
- Add `$this->loadComponent('AutoLogin.AutoLogin');` to `AppController::initialize()`
27+
- Add `$this->loadComponent('AutoLogin.AutoLogin');` to `AppController::initialize()` after `$this->loadComponent('Auth')`
2828
- Add `$this->AutoLogin->setCookie();` after `$this->Auth->setUser($user);` wherever you log your users in
2929
- Add `$this->AutoLogin->destroyCookie();` where you log your users out (e.g. in `UsersController::logout()`)
3030

src/Controller/Component/AutoLoginComponent.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class AutoLoginComponent extends Component {
1414

15-
public $components = ['Cookie', 'Auth'];
15+
public $components = ['Cookie'];
1616
public $request;
1717
public $settings = [];
1818
protected $_defaultConfig = [
@@ -30,7 +30,7 @@ public function initialize(array $config)
3030
{
3131
parent::initialize([]);
3232
$this->configureCookie();
33-
if ($this->config('autoLogin') == true && ! $this->Auth->user()) {
33+
if ($this->config('autoLogin') == true && ! $this->_registry->getController()->Auth->user()) {
3434
$this->restoreLoginFromCookie();
3535
}
3636
}
@@ -60,11 +60,11 @@ public function restoreLoginFromCookie()
6060
$controller = $this->_registry->getController();
6161
$tempRequest = $controller->request->data;
6262
$controller->request->data = $loginData;
63-
$user = $this->Auth->identify();
63+
$user = $controller->Auth->identify();
6464
$controller->request->data = $tempRequest;
6565

6666
if ($user) {
67-
$this->Auth->setUser($user);
67+
$controller->Auth->setUser($user);
6868
return true;
6969
}
7070

@@ -79,12 +79,12 @@ public function restoreLoginFromCookie()
7979
*/
8080
public function setCookie($data = [])
8181
{
82+
$controller = $this->_registry->getController();
8283
if (empty($data)) {
83-
$controller = $this->_registry->getController();
8484
$data = $controller->request->data;
8585
}
8686
if (empty($data)) {
87-
$data = $this->Auth->user();
87+
$data = $controller->Auth->user();
8888
}
8989
if (empty($data)) {
9090
return false;

0 commit comments

Comments
 (0)