PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 13, 2022

[FIXED] Auth.afterIdentify is not firing

 March 13, 2022     cakephp, cakephp-3.0     No comments   

Issue

I need to change something in the user session after it was started. This is temporary, so using an event such as Auth.afterIdentify is what I'm looking for.

What I tried

  • I have largely referred to this answer on how to even approach this.
  • I have tried using an anonymous function callback as well as a controller callback method
  • Following the Auth.afterIdentify documentation I have added implementedEvents
  • Made sure implementedEvents has a + parent::implementedEvents(), otherwise the controller kept showing me the home page

What I have

Here's my current src/Controller/AppController.php:

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

class AppController extends Controller implements \Cake\Event\EventListenerInterface
{

    public function initialize()
    {
        parent::initialize();

        // …

        $this->loadComponent('Authentication.Authentication');
        // Trying with an anonymous function
        \Cake\Event\EventManager::instance()->on('Auth.afterIdentify', function ($event) {
            Log::write( // noticed when posting this question, should have thrown an error
                'info',
                'Testing: ' . $event->getSubject()->id
            );
            debug($event);exit;
        });
        // Trying a controller callback
        \Cake\Event\EventManager::instance()->on('Auth.afterIdentify', [$this, 'afterIdentify']);
    }

    public function beforeFilter(\Cake\Event\Event $event)
    {
        parent::beforeFilter($event);
        $this->set('myAuth', $this->Authentication->getResult());
        $this->set('myUser', $this->Authentication->getIdentity());
    }

    public function afterIdentify(CakeEvent $cakeEvent, $data, $auth) {
        debug([
            '$cakeEvent' => $cakeEvent,
            '$data' => $data,
            '$auth' => $auth,
        ]);exit;
    }

    public function implementedEvents()
    {
        return [
            'Auth.afterIdentify' => 'afterIdentify',
        ] + parent::implementedEvents();
    }

}

What doesn't work

It seems neither of the above event listeners is being called. No CakePHP logs are being updated (not even with errors), although they normally work.

What I expected to happen

  • Calling Log::write without declaring where it comes from should have thrown (and logged) an error
  • The debug() information was not displayed
  • Removing the public function afterIdentify method should have caused an error; it didn't – meaning the controller isn't even looking for it

Solution

You are mixing up the old auth component and the new authentication plugin, the Auth.afterIdentify event belongs to the former.

The authentication plugin's authentication component has a Authentication.afterIdentify event, but this only applies to authenticators that are stateful and do not implement automatic persisting. So out of the box this only applies to the Form authenticator, and the event is being triggered once on the request where the user was authenticated via the form, on subsequent requests where they are authenticated via for example the Session authenticator, the event is not being triggered.

public function initialize()
{
    parent::initialize();

    // ...

    $this->loadComponent('Authentication.Authentication');

    $this->Authentication->getEventManager()->on(
        'Authentication.afterIdentify',
        function (
            \Cake\Event\EventInterface $event,
            \Authentication\Authenticator\AuthenticatorInterface $provider,
            \Authentication\IdentityInterface $identity,
            \Authentication\AuthenticationServiceInterface $service
        ) {
            // ...
            
            $identity['foo'] = 'bar';
            $this->Authentication->setIdentity($identity);
        }
    );
}


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing