Force login on Ajax requests

By Michal Szajbe (michal)
Users often encounter situations when they want to perform an action on a web site and get redirected to login page. Application’s task is to take care of performing the requested action right after user’s successful log in. It is easy job, unless the action is requested via ajax.

 

Comments 575

CakePHP Team Comments Author Comments
 

Comment

1 Another idea...

Hi,

Reading your article gave me an other idea.
  1. In the default layout you creat an an ajaxdiv. So it will encapsulate every pages of your web site and be available for ajax updates.
  2. In each view directory you put an ajax.ctp file that renders an element according to a name specified in a viewVar.
  3. In the elements directory you put your login.ctp file and your other_ajax_view.ctp
  4. In your login action you put something like : if login failed then set('viewVar','login') else set('viewVar','other_ajax_view')


If you need more information, i've written a tutorial about ajax elements working without session vars and using ajax element redirection : http://bakery.cakephp.org/articles/view/ajax-elements-available-anywere

Hope it helps!
Posted Dec 3, 2007 by Sami Radi