GWT: A way to cancel PlaceChangeEvent?

236 views Asked by At

I'm using Activity/Place in my GWT project, if current user is not logged in, when he navigates to some Place, the user will be redirect to login page, if the user has logged in, then he will be taken to that Place. How to implement this logic efficiently?

I tried to hook PlaceChangeRequestEvent:

        eventBus.addHandler(PlaceChangeRequestEvent.TYPE,new PlaceChangeRequestEvent.Handler() {
            @Override
            public void onPlaceChangeRequest(PlaceChangeRequestEvent event) {
                Place newPlace = event.getNewPlace();
                if (newPlace instanceof MyProtectedPlace && userNotLoggedIn()) {
                    event.goTo(new LoginPlace());
                }
            }
        });

Unfortunately it does not work since the ongoing request for MyProtectedPlace is not cancelled. Yes I could check this when user are about to navigation away from current place, but this will not be efficient as the check logic will scattered throughout the program.

Thanks.

1

There are 1 answers

0
Ronan Quillevere On

You can do it a little bit differently I think. Let's say that you want a place called SecuredPlace to be accessible only after login. You have a corresponding SecuredActivity.

What you can do is, when you start your SecuredActivity, you check if your user is logged in. If not you do placeController.goTo(new LoginPlace ()).

If the user is logged in then you continue. As the start is called by the framework there is no way to skip this step which in my opinion makes it secured enough.

But you should implement your security on network calls to your backend not on places. Every time you call the backend, you check that user is authenticated and has the right credentials. If not you can intercept the callback, check that it is a 403 error and then redirect automatically to your login page. Because if your backend calls are not secured, securing your places is useless.