Send user to Logout Page

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();

}

}

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.

Maaaybe this could help, implement this implements VaadinServiceInitListener
and in:

	 @Override
    public void serviceInit(ServiceInitEvent event) {
		event.getSource().  //here you would find a lot of "interesting listeners"
	}

Like I do to always have the last UI instance and control the users doesn’t skip the login

@Override
    public void serviceInit(ServiceInitEvent event) {
        System.out.println("serviceInit..." + event.getSource());
        event.getSource().addUIInitListener(evtt -> {
            ui = evtt.getUI();
            VaadinSession sess = ui.getSession();
            System.out.println("UI Init listener: " + ui + ", uiid=" + ui.getUIId()
                    + "\nse=" + sess + "\npushId=" + sess.getPushId()
                    + "\nafi=" + getCurrentAfiliado() == null
            );
            ui.addBeforeEnterListener(evt -> {
                String path = evt.getLocation().getPath();
                System.out.println(" before enter listener: ui=" + UI.getCurrent() + ", path=" + path + ", afi=" + (getCurrentAfiliado() != null));
                if (isLogged()) {
                    if (path.equals("login")) {
                        //si se quiere ir a login, ya logeado, vuelve al inicio
                        evt.forwardTo("");
                        
                    }
                } else {
                    if (!path.equals("login")
                            && !path.equalsIgnoreCase("recuperacionClave")
                            && !path.equalsIgnoreCase("registro")) {
                        //if try to skip the login
                        evt.forwardTo("login");
                        
                    }
                }
            });
        });
    }