summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMadhusudan.C.S2011-01-18 14:37:06 +0530
committerMadhusudan.C.S2011-01-18 14:37:06 +0530
commit8d21799aaab623727bbdd604c38e822f437d7b7e (patch)
tree9906ba985daeba41a41565699c42d3ba695658e1
parent545147d6017bc373169b7a9b20b8e6a1ba259f00 (diff)
downloadpytask-8d21799aaab623727bbdd604c38e822f437d7b7e.tar.gz
pytask-8d21799aaab623727bbdd604c38e822f437d7b7e.tar.bz2
pytask-8d21799aaab623727bbdd604c38e822f437d7b7e.zip
Handle all cases of login.
Firstly refactor the code to move all the post login mechanisms to a function of its own. Then add support to redirect the user if the login was attempted from logout page. Finally if the post response we got contains html, it is not a valid JSON, which means the login failed. In this case catch the exception and show the login error.
-rw-r--r--pytask/static/js/login.js41
1 files changed, 33 insertions, 8 deletions
diff --git a/pytask/static/js/login.js b/pytask/static/js/login.js
index 64fa522..31f32a3 100644
--- a/pytask/static/js/login.js
+++ b/pytask/static/js/login.js
@@ -1,17 +1,42 @@
var login_user = function (login_url) {
+
+ /* Function that handles the post login request changes. */
+ var process_login_response = function (raw_data) {
+ /* We expect an exception when login fails. Read comment with catch. */
+ try {
+ data = $.parseJSON(raw_data);
+ if (data.authentication == "success") {
+ /* Login succeeded */
+ if (data.markup) {
+ /* Replace the HTML with the user actions since
+ * the request came from a URL other than logout page */
+ $("div#useraction").replaceWith(data.markup);
+ } else if (data.redirect) {
+ /* Reload the page to the pytask home page since
+ * the login request came from logout page. This
+ * is done because the logout text says you have
+ * been logged out, which will be awkward after
+ * user re-logs in. */
+ window.location.href=data.redirect;
+ }
+ }
+ } catch (e) {
+ /* Login failed so the login view returned to the same view as
+ * the existing page from which the call was made and thus we
+ * get html. So let us display the error. */
+ $('div #loginform #error').show();
+ }
+ }
+
+
+ /* Attach a handler which does the form post upon the submit
+ * button is pressed on the login form. */
$(document).ready(function () {
$('#form_login').submit(function() {
$.post(
login_url,
$("#form_login").serialize(),
- function (raw_data) {
- data = $.parseJSON(raw_data);
- alert(data);
- if (data.authentication == "success") {
- $("div#useraction").replaceWith(data.markup);
- alert(data.markup);
- }
- });
+ process_login_response);
return false;
});
});