views:

68

answers:

2

When a user clicks a link that requires a log in, we currently redirect them to the log-in page ,but we lose the intended URL. What is the best way to account for that URL and redirect the user to the requested page once logged in?

We're using the latest stable version of Cake. Thanks.

--Edit--

Its configured like this

$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
// $this->Auth->autoRedirect = false;
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');

login function in users_controller

function login() {
    Debugger::log("Redirect Session Key:" . $this->Session->read('Auth.redirect'), $level = 7);
    $ref = $this->referer();
    Debugger::log("referer():" . $ref, $level = 7);
}

When I navigate to an unauthorized page like localhost/mysite/unauthorized I am redirected to localhost/mysite/users/login. In my debug.log file The Redirect Session Key prints out as users/dashboard which implies that there is no valid referrer. To validate that assumption I also print out the referrer in the login function which does in fact return "/" which is the default value in the case of no referrer information from the requester I believe.

A: 

Where is the configuration for AuthComponent and how should it be configured?

Hooman Ahmadi
+1  A: 

"According to the cake documentation the authcomponent only preserves this in the session if the user is not coming from an external link."

Read the section again:

The AuthComponent remembers what controller/action pair you were trying to get to before you were asked to authenticate yourself by storing this value in the Session, under the Auth.redirect key. However, if this session value is not set (if you're coming to the login page from an external link, for example), then the user will be redirected to the URL specified in loginRedirect.

It will remember the page you were trying to access, unless you went to the login page directly and hence there's no previous page you came from. If you link to /foo/secret_page and are redirected to the login page though, /foo/secret_page should be remembered in the session.

If you get rid of the $this->Auth->autoRedirect = false and let the AuthComponent do its thing, it should work the way you want.

Otherwise you can read and set the 'Auth.redirect' session key manually to have more control over redirects.

deceze
If I get rid of the "$this->Auth->autoRedirect = false"the login function won't get processed correct? At least thats what it seems to imply in the documentation.Also, even when a successful login occurs, this statement which exists inside the login() function is displaying a null value. "Debugger::log($this->Auth->user(), $level = 7);"Any idea what is going on here?Thanks!
wcolbert
@wcolbert Please post you complete `login` function as well then.
deceze
I posted it. I have stripped down the function greatly to try to pin point the problems. It seems as though when I navigate to a page in the browser, the intended page does not show up as a referrer as I would expect. It is also not being passed to the Auth.Redirect session key.
wcolbert
Even with the login function essentially commented out, there is no redirect to /foo/secret_page
wcolbert
Issue has been resolved. There was a redirect being made in a controller else where. Thanks for all your help.
wcolbert
@wcolbert Glad it worked. Redirection issues can be tricky to debug... :)
deceze