Morris
Morris

Reputation: 142

Couchdb jquery api add Authorization Header

I want add the Authorization Header to the login request. I try to do that with the code:

function dblogin (credential){
            $.couch.login({
                name:credential.name, 
                password:credential.password,
                headers: {'Authorization': 'Basic ...'},
                success: function (data){console.log("loggato con successo!");},
                error: function(status) {console.log("login non riuscito!" + status);}
            });
        }

But I see that into HTTP request header the field Authorization is not present. Why?

Upvotes: 0

Views: 458

Answers (1)

Paul Mougel
Paul Mougel

Reputation: 17038

From the source code of jquery.couch.js, you'll see that the login function doesn't use every parameter you send it:

login: function(options) {
  options = options || {};
  $.ajax({
    type: "POST", url: this.urlPrefix + "/_session", dataType: "json",
    data: {name: options.name, password: options.password},
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Accept', 'application/json');
    },
    complete: function(req) {
      var resp = $.parseJSON(req.responseText);
      if (req.status == 200) {
        if (options.success) options.success(resp);
      } else if (options.error) {
        options.error(req.status, resp.error, resp.reason);
      } else {
        throw 'An error occurred logging in: ' + resp.reason;
      }
    }
  });

So there's actually no way to send it custom headers.

Edit: And that's actually a shame, since they define their own ajax function wrapper which allows header customization. But for some reason they don't use it in some particular functions, including login...

Upvotes: 2

Related Questions