I am enforcing a user logout, by programatically calling authenticationContext.logout() after a successful password change, and this works correctly.
However, to have a better UX, I also want to show a Notification and then force the logout after a delay of say 1500.
I tried using using this, but the Notification never shows up on the screen.
try {
Notification.show("Your password was successfully changed!",5000,
Notification.Position.MIDDLE).addThemeVariants(NotificationVariant.LUMO_ERROR);
Thread.sleep(1500);// Small delay
authenticationContext.logout();//
} catch (InterruptedException e) {
e.printStackTrace();
}
You need to enable Server Push. By default the server sends UI changes only when the code is processed. When push is enabled you can send the changes earlier or event asynchronous.
Then you can use add push like this:
try {
Notification.show("Your password was successfully changed!",5000,
Notification.Position.MIDDLE).addThemeVariants(NotificationVariant.LUMO_ERROR);
UI.getCurrent().push();
Thread.sleep(1500);// Small delay
authenticationContext.logout();//
} catch (InterruptedException e) {
e.printStackTrace();
}
Thank you so much. I was not even aware Notification accepted listeners. Duh!
Overall I found this approach more elegant than my initial thought of using threads.
Here is how I got it working.
//Build and show notification
Notification notification = new Notification("", 1500, Notification.Position.MIDDLE);
notification.addThemeVariants(NotificationVariant.LUMO_SUCCESS);
notification.add(
new Div(
new Html("<b>Your password was successfully changed!</b>"),
new Html("<br>"),
new Text("You will now be logged out")
)
);
//Logout after notification closes
notification.addOpenedChangeListener(e -> {
if (!e.isOpened()) {
authenticationContext.logout();
}
});
//Show notification
notification.open();
Are there some performance issues with using html?
What’s the alternative then to get a two line message, one of them bold?
Should I be using a combination of Span / Div with adjusted font-weight via getStyle().set() instead?
But this can lead to a potential security issue: close the UI where the notification is shown, and then the logout never happens. Not sure if that can actually be used for anything though. And the fix to that issue might be to have a server-side timer with a slightly longer delay as a fallback.