Commit 49ade6377ec31e8ff12105cbac07541d5232f0fe

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.
  • Diff rendering mode:
  • inline
  • side by side

static/sweet-authenticate.js

1var sweet = {1var sweet = {
2 authenticate: function(url,user, hash){
3 $.post(url,{"user":user, "hash":hash}, function(data){
4
5 return true;
6 });
2 // takes in sweet store URL and username and password and a success callback
3 // and an error callback,
4 // and authenticates the user with the given sweet store.
5 // If the user is authenticated, the callback is executed, passing in the
6 // response from sweet store. Else, the error cb is executed.
7 authenticate: function(url, user, hash, cb, errorCb) {
8 // assuming jquery is available
9 // TODO: fix this
10 $.ajax({
11 url: url,
12 type: 'POST',
13 data: {'user': user, 'hash': hash},
14 success: function(data, textStatus) {
15 cb(data);
16 },
17 error: function(jqxhr, textStatus, error) {
18 alert('Authentication failed! Please check your username and password');
19 console.log(error, textStatus);
20 errorCb(textStatus, error);
21 }
22 });
7 }23 }
8
9};24};

swtr.py

207 response.headers['Access-Control-Allow-Origin'] = '*'207 response.headers['Access-Control-Allow-Origin'] = '*'
208 return response208 return response
209 else:209 else:
210 response.status_code = 403
211 response.headers['Access-Control-Allow-Origin'] = '*'
212 return response
210 pass
211 response.status_code = 403
212 response.headers['Access-Control-Allow-Origin'] = '*'
213 return response
213 elif request.method == "GET":214 elif request.method == "GET":
214 return app.send_static_file("sweet-authenticate.js")215 return app.send_static_file("sweet-authenticate.js")
215216