Skip to content

Commit

Permalink
Merge pull request #120 from dereuromark/feature/login-event
Browse files Browse the repository at this point in the history
Add a login event for login counter and flash message for login success.
  • Loading branch information
ADmad committed Jul 11, 2018
2 parents 64f0bb0 + 2b78d22 commit 9864e06
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/composer.lock
/phpunit.xml
/vendor
/.idea/
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,19 @@ event. You must setup a listener for this event which save new user record to
your users table and return an entity for the new user. Here's how you can setup
a method of your `UsersTable` as callback for the event.

If you also want to monitor all logins - and execute e.g. a login counter - you can listen for the `HybridAuth.login` event.

```php
public function initialize(array $config)
{
$this->hasMany('ADmad/HybridAuth.SocialProfiles');

\Cake\Event\EventManager::instance()->on('HybridAuth.newUser', [$this, 'createUser']);
\Cake\Event\EventManager::instance()->on('HybridAuth.login', [$this, 'updateUser']);
}

public function createUser(\Cake\Event\Event $event) {
public function createUser(\Cake\Event\Event $event)
{
// Entity representing record in social_profiles table
$profile = $event->data()['profile'];

Expand All @@ -196,6 +200,30 @@ public function createUser(\Cake\Event\Event $event) {

return $user;
}

public function updateUser(\Cake\Event\Event $event, array $user)
{
$this->updateAll(['logins = logins + 1', 'last_login' => new FrozenTime()], ['id' => $user['id']]);
}
```

Additionally, you can also get a flash message for login back using the `HybridAuth.login` event:
```php
// In your AppController
public function initialize()
{
EventManager::instance()->on('HybridAuth.login', [$this->MyComponent, 'updateUser']);
}

// In your MyComponent
public $components = [
'Flash'
];

public function updateUser(Event $event, array $user)
{
$this->Flash->success(__('You are now logged in'));
}
```

Twitter & email addresses
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/HybridAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function authenticated()
if ($user) {
$this->Auth->setUser($user);

$this->dispatchEvent('HybridAuth.login', ['user' => $user]);

return $this->redirect($this->Auth->redirectUrl());
}

Expand Down

0 comments on commit 9864e06

Please sign in to comment.