6 Replies Latest reply on Apr 26, 2010 11:03 AM by Michael Wohlfart

    Best way to redirect user after login, no specific loginpage

    Henrik Troeng Newbie

      Hi, I have a login.xhtml page which is included in a template, so a user can login from any page in the application.
      Now I want to redirect the user to a specific page like /my/index.xhtml, and it would be rather easy if I had a single login page, but how would I redirect the user now, when I dont know from which page the user logs in?


      I guess it would work to add a



      <navigation from-action="#{identity.login}">
      <redirect view-id="/my/index.xhtml"/>
      </navigation>





      tag on every page element, but I have a lot of pages and would like to do it in a more generic way...



        • 1. Re: Best way to redirect user after login, no specific loginpage
          Adrian Mitev Master

          Put the navigation rule in:


          <page view-id="*">
          

          • 3. Re: Best way to redirect user after login, no specific loginpage
            Alain Hoang Newbie

            Hello,


            Do you have a trick to redirect to same page that requires a login after user get through login page?


            I do have several pages that requires login:


            in pages.xml:



             <page view-id="/page1.xhtml" login-required="true" >
              <rewrite pattern="/page1" />
             </page>
             <!-- ... -->
             <page view-id="/page9.xhtml" login-required="true">
              <rewrite pattern="/page9" />
             </page>
            






             <page view-id="/login.xhtml" action="#{''.toString()}" >
                    <navigation from-action="#{''.toString()}">
                            <rule if="#{identity.login}">
                                    <redirect view-id="/*"/>
                            </rule>
                    </navigation>
                    <rewrite pattern="/login" />
             </page>
            







            the


            <redirect view-id="/*"/>



            does not work.


            Do you have any suggestion?


            See you,
            Alain.






            • 4. Re: Best way to redirect user after login, no specific loginpage
              Michael Wohlfart Expert

              Hi Alain,


              I had some trouble with the action="#{''.toString()}" hack in JSF2,  so my setup in pages.xml looks like this:


                  <page view-id="/pages/login.xhtml" 
                        action="#{identity.isLoggedIn()}"
                        conversation-required="false" 
                        login-required="false">     
                    <description>login page</description> 
                    <navigation from-action="#{identity.isLoggedIn()}">
                      <rule if-outcome="true" >
                        <redirect view-id="/pages/user/home.xhtml" />
                      </rule>
                      <rule if-outcome="false" >
                        <render view-id="/pages/login.xhtml" />
                      </rule>
                    </navigation>
                  </page>



              to redirect to the page that required the login you need this in components.xml:




                <!-- capture the view on the not login security event -->
                <event type="org.jboss.seam.security.notLoggedIn">
                  <action execute="#{redirect.captureCurrentView}"/>
                </event>
                 
                <!-- redirect to the captured view after login  -->
                <event type="org.jboss.seam.security.postAuthenticate">
                  <action execute="#{redirect.returnToCapturedView}"/>
                </event>






                  

              • 5. Re: Best way to redirect user after login, no specific loginpage
                Alain Hoang Newbie

                Hello,


                How could I use the redirect function like getViewID() from foo.xhtml in JBoss-Seam-Richfaces environment?


                I try this:



                <h:outputText value="Succesfull login: redirect to #{org.jboss.seam.faces.redirect.getViewId}."/>





                I'm getting this error:



                18:31:36,683 ERROR [viewhandler] Error Rendering View[/login.xhtml]
                javax.faces.FacesException: javax.el.PropertyNotFoundException: /login.xhtml @168,109 value="Succesfull login: redirect to #{org.jboss.seam.faces.redirect.getViewId}.": Property 'getViewId' not found on type org.jboss.seam.faces.Redirect
                     at javax.faces.component.UIOutput.getValue(UIOutput.java:187)




                Note: I did override the authenticate function. Should I add send of events like org.jboss.seam.security.postAuthenticate ?


                See you,
                Alain.



                • 6. Re: Best way to redirect user after login, no specific loginpage
                  Michael Wohlfart Expert

                  I am not sure what you try to do, but


                  <h:outputText value="Succesfull login: redirect to #{org.jboss.seam.faces.redirect.getViewId()}."/>



                  gets rid of the


                  Property 'getViewId' not found on type org.jboss.seam.faces.Redirect


                  error.


                  But this doesn't give you a redirect, it just prints some view id.


                  The org.jboss.seam.security.postAuthenticate event is triggered by Seam.