Search code examples
zend-framework2zfcuser

How would I log a successful login


How would I log a successful login within the zfc-user module for the purpose of showing the user the last time he/she was successfully authorized?

I'm thinking a listener as zfcuser uses events, but are unable to figure it out


Solution

  • There's an authenticate event created in the module, which you can hook into (check example inside AdapterChainServiceFactory)

    $chain = new AdapterChain;
    $adapter = $serviceLocator->get('ZfcUser\Authentication\Adapter\Db');
    $chain->getEventManager()->attach('authenticate', array($adapter, 'authenticate'));
    

    You can hook onto the event, and then do some logging if the authenticate was a success, something like this:

    // Attach onto the event somewhere...
    $chain->getEventManager()->attach('authenticate', array($myObject, 'logAuthenticate'));
    
    // MyObject
    public function logAuthenticate(AuthEvent $e)
    {
        // logging magic here..
    }