PDA

View Full Version : Authentication (AJAX) - Best practice



eniu
20 Aug 2011, 1:42 AM
Hi there

I'm quite new to ExtJs 4. I'm searching for the best practice to handle authentication in ExtJs. I'm able to authenticate with my server but if the session times out and I make an AJAX request (via. json store / AJAX proxy), the server returns the HTTP status 401 (Unauthorized) and no rows were displayed in my grid. How can I catch and handle this HTTP status? If I just display the login form to authenticate there is the problem that there may be multiple AJAX calls with status 401 and the login form is display for each call.

Any ideas?

Sorry for my bad english.

Regards Sven

christocracy
22 Aug 2011, 5:12 AM
See the docs for Ext.Ajax: http://docs.sencha.com/ext-js/4-0/#/api/Ext.Ajax

Attach a listener to #requestcomplete and/or #requestexception.

Event-handlers attached to Ext.Ajax will fire for every ajax request in the app. It's always a good idea to wire-up some sort of application-wide error-handling of this sort.

skirtle
22 Aug 2011, 8:05 PM
If I just display the login form to authenticate there is the problem that there may be multiple AJAX calls with status 401 and the login form is display for each call.

Though it's not clear from what you've said, it sounds like you're proposing trying to authenticate the request. This might be possible but far more common is just to logout the user and show them the login screen. If the login screen is a completely separate page then the problem with multiple Ajax requests is solved. If it isn't a separate page then you may need to manually build logic into your error handling to ensure you don't try to reshow the login form if it's already visible.

Having a global error handler for Ajax requests as suggested by christocracy is definitely a good idea.

eniu
23 Aug 2011, 8:10 AM
Yes I tried to authenticate the request but I think it would get far to complex. Now I'm just redirecting to the login form. Thank you for your support.

Best regards
Sven