A user can log themselves out, or they should be logged out after inactivity. The idea is that after 3 heart beats, Vaadin will destroy the session due to inactivity. That’s great and works, however, the UIs are closed which prevents the browser from forwarding to: https://mysite.com/logout
This logic is dervied from this page: https://vaadin.com/docs/flow/advanced/tutorial-application-lifecycle.html
But even in the above vaadin page, it’s not accurate…because it refers to deprecated logic ( @VaadinServletConfiguration) and that seems overkill anyway for such basic and expected logic…
Doesf Vaadin provide a way (listener?) BEFORE it destroys a UI, then I could place my logic there…but I don’t see how to properly log a user out due to inactivty.
package com.mysite.ui.authentication;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.DetachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.page.Push;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.*;
@Route(“”)
@PageTitle(“Login”)
@Push
public class LoginScreen extends VerticalLayout {
public LoginScreen() {
}
void logout() {
System.out.println("in logout");
UI.getCurrent().getPage().executeJs("window.location.href='https://mysite.com/logout'"); //this does not occur.
CurrentUser.userSignOut();
}
@Override
public void onAttach(AttachEvent attachEvent) {
System.out.println("In onAttach");
Button logout = new Button("Logout");
logout.addClickListener(buttonClickEvent -> {
VaadinSession.getCurrent().close();
});
add(logout);
UI.getCurrent().getInternals().addHeartbeatListener(heartbeatEvent -> {
System.out.println("Heartbeat: " + heartbeatEvent.getSource());
});
}
@Override
public void onDetach(DetachEvent detachEvent) {
System.out.println("In onDetach");
logout();
}
}