Version 4

    Sometimes after the user has been logged in, he returns the login page (as instance when he clicks a bookmark in the browser). But the user already is logged in, it will be desirable to show him the main page (or whatever) instead the login page.

     

    To acomplish this you can execute an page action which is executed before rendering the login page, then use a navigation rule to redirect the page you want if the user is logged in, all this stuff in the pages.xml.

     

    Your pages.xml for the login page should look like:

     

    <page view-id="/login.xhtml" action="#{authenticator.checkIfLoogedIn}">
            <navigation from-action="#{authenticator.checkIfLoogedIn}">
                <rule if="#{identity.loggedIn}">
                    <redirect view-id="/home.xhtml"></redirect>
                </rule>
            </navigation>
            <navigation from-action="#{identity.login}">
                <rule if="#{identity.loggedIn}">
                    <redirect view-id="/home.xhtml"></redirect>
                </rule>
            </navigation>
    </page>
    
    

     

    And if required, you authenticator class could look like:

    @Name("authenticator")
    public class AuthenticatorBean{
    
        @In
        private EntityManager entityManager;
    
        @Logger
        private Log log;
    
        @In(required = false, scope = ScopeType.SESSION)    
        @Out(required = false, scope = ScopeType.SESSION)
        private User currentUser;
    
       
    
        @In
        private Identity identity;
    
    
        /**
         * Authenticate an user in the application
         *
         * @return true if is authenticated, false otherwise
         */
        public boolean authenticate() {
            try {
    
                currentUser = (User) entityManager.createQuery("select u from User u where u.username=:username and u.password=:password")
                        .setParameter("username", identity.getUsername())
                        .setParameter("password", identity.getPassword())
                        .getSingleResult();
    
                if (currentUser.getRoles() != null) {
                    for (Role role : currentUser.getRoles()) {
                        identity.addRole(role.getName());
                    }
                }
    
                return true;
            }
            catch (NoResultException ex) {
                return false;
            }
        }
    
        public void checkIfLoogedIn() {
            log.debug("Method called to force the redirection to home if user currently is logged in");
    
        }
    }
    

     

     

    You can also use the page action to invoke the identity.login action before render the login page. By instance when you don't want a login form page, and you are using another mechamisn to get the username and password. The code should look like (in pages.xml):

     

    <page view-id="/login.xhtml" action="#{identity.login}">
            <navigation from-action="#{identity.login}">
                <rule if="#{identity.loggedIn}">
                    <redirect view-id="/home.xhtml"></redirect>
                </rule>
            </navigation>
    </page>