Skip to main content

Update User Entity on login

I┬árecently built an app that required we update the authenticated user’s lastOnlineDate every session so we can quickly track who has been online in the last interval of time. We set this interval as a parameter and used that to avoid hitting the database more than absolutely necessary to accomplish this.

To make it work, we needed to leverage onSecurityInteractiveLogin:

app/config/parameters.yml

MyApp/EventListener/LoginListener.php

 

app/config/services.yml


Alternatives

In this particular use-case, leveraging a setContainer call may not be necessary if all you’re doing is setting a single value on a User entity. Alternatively, we could simply send the activity_minutes parameter as an argument to the service, and not use the container at all.

We’d have to adjust our LoginListener appropriately as well: