To efficiently limit access to a page, we can use the onActivate
method. You should be familiar with this method from Chapter 3 where it was used to pass a value through page activation context. The onActivate
method is invoked every time the page is loaded, and if there is some value in the activation context, it will be passed as an argument to this method. However, another use for it might be to check whether the user who tries to access the page was successfully authenticated.
If you remember, in the case of a successful authentication, we are storing the User
object as an ASO. So all we need to do in order to check whether a user was authenticated is to check whether the User
ASO exists. Add the following code to the ShowAll
page class:
@ApplicationState private User user; private boolean userExists; Object onActivate() { if (!userExists) return Start.class; return null; }
Everything is very simple. We are checking the value of the boolean variable associated...