Sunday, August 20th, 2006
Ajax Login with Acegi
<p>Sanjiv Jivan has written about how he has integrated an ajax based login form to the Acegi Spring module for Auth and Auth.His implementation contains a ServletFilter in J2EE land, and JavaScript in the client:
-
-
function ajaxLogin() {
-
Element.update('loginMessage', 'Sending request ...');
-
Element.show('loginMessage');
-
var opt = {
-
-
method: 'post',
-
-
postBody: Form.serialize($('loginForm')) + '&ajax=true',
-
-
-
onSuccess: function(response) {
-
var msg = response.responseText;
-
if ("error:" == msg.substr(0, 6)) {
-
var fp = "<font color='red'>" + msg.substring(6, msg.length) + '</font><br /><a href="forgetPwd.html">Forget Password?</a>';
-
Element.update('loginMessage', fp);
-
} else if ("url:" == msg.substr(0, 4)) {
-
location.href = msg.substring(4, msg.length);
-
} else if ("message:" == msg.substr(0, 8)) {
-
Element.update('loginMessage', msg.substring(8, msg.length));
-
}
-
}
-
}
-
new Ajax.Request('j_acegi_security_check', opt);
-
}
-
Related Content:












FONTtags? Please. Aren’t they illegal yet?and this is special why…
I’m somewhat disappointed by this client-side code. I guess it’s just an example, but font tags and use of substr as opposed to returning JSON or XML formatted data seems kind of weak. I guess it may not be what’s of primary importance, but I figure if you’re gonna show an example, try to do everything as elegantly as possible in all parts of the code.
I agree that the client side code is not the most elegant or complete, but the point of the blog entry was to demonstrate how to implement an Ajax login when web containers redirect on failed or successful login which normally forces a browser refresh. The emphasis was on the server side handling and leave it up to the user to implement a fontend that meets their needs.
I wanted to keep the client code as simple as possible leaving it up to the user to use the techniques illustrated to build their own login screen. The sample also uses a simple hidden div to show the login dialog, something which I would never use for a real app. I would rather use a Dojo dialog for instance. Also the user can in their client code make another Ajax request and refresh a “body div” instead of simply doing location.href = msg.substring(4, msg.length);
I agree that the client side code is not the most elegant or complete, but the point of the blog entry was to demonstrate how to implement an Ajax login when web containers redirect on failed or successful login which normally forces a browser refresh. The emphasis was on the server side handling and leave it up to the user to implement a fontend that meets their needs.
I wanted to keep the client code as simple as possible leaving it up to the user to use the techniques illustrated to build their own login screen. The sample also uses a simple hidden div to show the login dialog, something which I would never use for a real app. I would rather use a Dojo dialog for instance. Also the user can in their client code make another Ajax request and refresh a “body div” instead of simply doing location.href = new url.
“Ajax login when web containers redirect on failed or successful login which normally forces a browser refresh.”
And i thought ajax had something to do with apple pie.