Flow: Add rerouteToUrl(..) method to BeforeEvents

Created on 14 Sep 2018  路  3Comments  路  Source: vaadin/flow

BeforeEvents are handy for taking care of different cases needing rerouting. Typical use case is using BeforeEnterEvent with View access control and rerouting to login if needed. However rerouteTo(..) method as described here cannot take external URL as parameter, and in many corporate apps SSO is used, which means that login page is external. Hence I propose to add rerouteToUrl(String url) methdo that will fill this gap. It is possible to workaround this e.g. this way

@Override
public void beforeEnter(BeforeEnterEvent event) {
    if (VaadinSession.getCurrent().getAttribute("userLoggedIn") == null) {
        UI.getCurrent().getPage().executeJavaScript("window.open(\"http://vaadin.com/\", \"_self\");");
    }
}

enhancement routing

Most helpful comment

I created a bit of a workaround for this case where I have an intermediate view that will encode the target location (url path and query parameters) as a URL parameter. Once it entered, it decodes the target and uses ui.navigate(location, params) to update the navigation. This gets the desired effect and prevents the original navigation because it uses the BeforeEnterEvent.rerouteTo method.

ForwardToWithQueryParametersView.java.zip

All 3 comments

It would be useful to have also a possibility to give query parameters to forwardTo call for instance like below:
beforeEvent.forwardTo(LoginView.class, queryParams)

I created a bit of a workaround for this case where I have an intermediate view that will encode the target location (url path and query parameters) as a URL parameter. Once it entered, it decodes the target and uses ui.navigate(location, params) to update the navigation. This gets the desired effect and prevents the original navigation because it uses the BeforeEnterEvent.rerouteTo method.

ForwardToWithQueryParametersView.java.zip

It would be nice to have built-in support to redirect to external URLs (or rather URLs that are not routes) within the context of a BeforeEnterEvent.

Page#setLocation(...) within BeforeEnterListener did essentially work for me, BUT Vaadin shows the original target before going to the new URL. So one has to reroute to what is essentially a dummy view in order to not show anything the user shouldn't see (or to prevent unnecessarily loading the original view).
Another bit of annoyance that comes into play here is that even for rerouting, where the location isn't changed, the target component still needs a route. But I created a separate issue for that: #9386

Was this page helpful?
0 / 5 - 0 ratings