Using ColdFusion Model-Glue, how do I redirect the user back to their requested page after logging in?

620 Views Asked by At

I have a secured event type, which will check to see if the user is logged in, and the redirect them to the login page if they're not:

<event-types>
    <event-type name="securedPage">
        <before>
            <broadcasts>
                <message name="CheckLogin"/>
            </broadcasts>
        </before>
        <results>
            <result name="NeedLogin" do="page.login" redirect="true"/>
        </results>
    </event-type>
</event-types>

The login page has a form that links to an action.login event handler, which will redirect to the user's homepage if login was successful, and redisplay the login page if it wasn't:

<event-handler name="action.login">
    <broadcasts>
        <message name="Login"/>
    </broadcasts>
    <results>
        <result name="LoggedIn" do="page.user.home" redirect="true"/>
        <result name="NotLoggedIn" do="page.login"/>
    </results>
</event-handler>

This code works great, but the problem I'm running into now is that it will always redirect to the user's homepage, even if they requested another page. Since the form posts to the action.login event, and the result do is hard-coded to page.user.home, how can I modify my code to keep track of which page the user originally requested and redirect them back there?

2

There are 2 best solutions below

1
On BEST ANSWER

In the logic you use to determine if a user needs to be logged in, you can grab the current event and store it in a shared scope (like the session scope).

Then, after a successful login simply call event.forward() and pass in that value.

0
On

here's the general idea:

  1. in whatever method/code runs to 'secure' your page, if a user is not logged in, grab the url they were trying to reach and set it back to the viewstate as a variable before you forward them. the CGI scope will be your friend here, particularly CGI.query_string.

  2. on your sign/log in form, check for this 'returnEvent' variable in the viewstate and set it as a hidden value on your form if it exists.

  3. someplace in your sign up/in process after success, check for this 'returnEvent' variable and if it exists, forward the user on.