Now I'm training on event management on Symfony.
So far everything is going well!
I have the following code:
When a user connects to the application, a listerner detects the connection event and updates the last_login_at field in bdd
So far all is going well.
The problem appears when the user wants to log out.
Indeed, when disconnected, the listener's onSecurityAuthenticationSuccess method is always called.
Problem once disconnected we can no longer recover a user (in the session)
Consequence: that bug!
What I would like:
It is able to record the date of the last connection in the database called the onSecurityAuthenticationSuccess method only on login and not on login and logout In addition I have an is_in_line field in the database and I would like to disconnect, I can detect the event "disconnection" and update this field How can I do this?
Here is my code to list it
public function onSecurityAuthenticationSuccess (AuthenticationEvent $ event) {
$ user = $ event-> getAuthenticationToken () -> getUser ();
if ($ user) {
// dd ($ user);
$ user-> setLastLoginAt (new \ Datetime ('now'));
$ this-> em-> persist ($ user);
$ this-> em-> flush ();
}
}