Properly calling VaadinSession.close() refreshes UI

When user clicks on sign out button, we call logOut() in UI class:

// Setting our own session attribute to null first
VaadinSession.getCurrent().setAttribute(MainView.CURRENT_USER, null);
UI.getCurrent().navigate(LoginView.class);
VaadinSession.getCurrent().close();

This will redirect user to the login page, but when they try to type in the login form (textfield), the UI refreshes again. If we remove VaadinSession.getCurrent().close(); the UI doesn’t refresh, but the session won’t properly close.

How can we properly close the session without the UI refreshing again?

I would recommend to do it differently. Do something like below on logout, which will invalidate the session properly and reload with default route.

VaadinSession.getCurrent().setAttribute(MainView.CURRENT_USER, null);
VaadinSession.getCurrent().getSession().invalidate();
UI.getCurrent().getPage().executeJavaScript("window.location.href=''");

And then in your routes and navigation have redirection to login view in case user is not logged in.

Tatu Lund:
I would recommend to do it differently. Do something like below on logout, which will invalidate the session properly and reload with default route.

VaadinSession.getCurrent().setAttribute(MainView.CURRENT_USER, null);
VaadinSession.getCurrent().getSession().invalidate();
UI.getCurrent().getPage().executeJavaScript("window.location.href=''");

And then in your routes and navigation have redirection to login view in case user is not logged in.

Thank you, Tatu! This worked like a charm!