How to deal with Ajax Auth errors in CakePHP
I'm using the Auth component on my pages and I would like to show a login overlay when an Ajax action is called for which the user doesn't have permission.
Example:
User wants to edit a jeditable field, then when the users wants to change the value, he will first get a login/register overlay.
My current way is to check in the action for $_SESSION['Auth']['User'], however this seems to be very dirty and I want to do it in a clean way.
Currently my code is:
if(!$_SESSION['Auth']['User']) {
$this->layout = 'ajax';
$this->render('loginpopup');
return;
}
I would love the input of more experienced users开发者_开发百科, I'm using Jquery as my javascript library. Thanks in advance!
I don't know cake, but in terms of best practice, your application should probably have a universal helper method for accessing the session. The rails standard is current_user
(which returns a user object if someone is logged in, and nil if not). Then you should wrap javascript to launch the login popup in a PHP conditional which calls that method.
精彩评论