javacuba-platform

Creating a listener for UserSessions (CUBA Studio)


I am creating a project on the CUBA Studio platform (Java). I will be grateful for tips and useful links.

I have the MyUser extends User class, the field lastTimeOnline has been added. I want to implement the update of this field through the UserSession (activation and deactivation). The UserSession itself is not saved in the database.

Cuba Studio has a UserSessionListener interface that I would like to implement, but I can't figure out how to activate this listener.


Solution

  • Use Spring listeners for authentication events provided by CUBA. E.g. for tracking a successful login, implement a Spring bean:

    @Component
    public class LoginEventListener {
    
        @EventListener
        protected void onUserLoggedIn(UserLoggedInEvent event) {
            User user = event.getUserSession().getUser();
            // ...
        }
    }
    

    More info here: https://doc.cuba-platform.com/manual-7.2/login.html#login_events

    Though I am not sure if corresponding event exists for session expiration.