From 49ade6377ec31e8ff12105cbac07541d5232f0fe Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Wed, 3 Jul 2013 17:12:45 +0530 Subject: [PATCH] Fix errors in authentication - Fix error in authentication. The condition to send error reponse was inside the for loop, which would always fail if the condition doesnt satisfy in the first iteration. - Add callback support in sweet-authenticate.js, it cannot return a value as it is async. --- static/sweet-authenticate.js | 29 ++++++++++++++++++++++------- swtr.py | 7 ++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/static/sweet-authenticate.js b/static/sweet-authenticate.js index 0f36a84..0cec8e3 100644 --- a/static/sweet-authenticate.js +++ b/static/sweet-authenticate.js @@ -1,9 +1,24 @@ var sweet = { - authenticate: function(url,user, hash){ - $.post(url,{"user":user, "hash":hash}, function(data){ - - return true; - }); + // takes in sweet store URL and username and password and a success callback + // and an error callback, + // and authenticates the user with the given sweet store. + // If the user is authenticated, the callback is executed, passing in the + // response from sweet store. Else, the error cb is executed. + authenticate: function(url, user, hash, cb, errorCb) { + // assuming jquery is available + // TODO: fix this + $.ajax({ + url: url, + type: 'POST', + data: {'user': user, 'hash': hash}, + success: function(data, textStatus) { + cb(data); + }, + error: function(jqxhr, textStatus, error) { + alert('Authentication failed! Please check your username and password'); + console.log(error, textStatus); + errorCb(textStatus, error); + } + }); } - -}; \ No newline at end of file +}; diff --git a/swtr.py b/swtr.py index 0549d00..7b60db1 100644 --- a/swtr.py +++ b/swtr.py @@ -207,9 +207,10 @@ def authenticate(): response.headers['Access-Control-Allow-Origin'] = '*' return response else: - response.status_code = 403 - response.headers['Access-Control-Allow-Origin'] = '*' - return response + pass + response.status_code = 403 + response.headers['Access-Control-Allow-Origin'] = '*' + return response elif request.method == "GET": return app.send_static_file("sweet-authenticate.js") -- 1.7.10.4