Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

How to listen to the log out event to record the event on the database?

$
0
0

When a user connects to the application, a listener detects the connection event and updates the last_login_at field for the user.

The problem appears when the user wants to log out.

When disconnected, the listener's onSecurityAuthenticationSuccess method is always called.

Problem once disconnected we can no longer recover a user (in the session)

What I would like is to be 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 ();
    }
}

Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>