views:

378

answers:

2

I have a link on the main page that is only accessible if they are logged in. However, if this link is clicked, I want to show a custom error message on the login page (a custom 'Message.auth').

i.e. I want (pseudo code)

if (referer == '/users/reserve'){
    Message.auth = 'Please log in to reserve tickets';
}
else {
    Message.auth = 'Please log in to access that page';
}

Where would I put this bit of code?

+1  A: 

to get referer you can call $this->referer() to get the referring URL then pass that value to your view.

see: referer

centr0
+1  A: 

Provided you have auth flash messages being output in the login view, this should work:

// login action of users_controller.ctp
if ($this->Session->check('Auth.redirect')
 && $this->Session->read('Auth.redirect') == '/users/reserve') {
  $this->Session->write('Message.auth', 'Please log in to reserve tickets');
}
neilcrookes
i actually had to use a preg_match on the redirect, because there is usually stuff after the reserve, but that was the key. thanks.
contagious
also, i had some strange issues with /tmp/cache when using the Session->write. It's easier to use `setflash("", 'default', array(), 'auth')` to do this.
contagious