This plugin enables access to remote resources which are protected by HTTP Basic Authentication through your Backbone Models and Collections.
Usage:
var Model = Backbone.Model.extend({
url: 'http://path/to/basic/auth/protected/resource'
});
Backbone.BasicAuth.set('username', 'password');
var model = new Model();
model.fetch();
A resource protected with HTTP Basic Authentication requires the following HTTP header to be set on every request:
Authorization: Basic <accesstoken>
The access token is formed by taking the username and password, concatenating together with a :
separator and encoding into Base64.
This plugin handles the Base64 encoding and automatically sets the Authorization
header on every request which uses Backbone.sync
.
Sets the access token which is used by all future remote requests, until clear()
is called.
Clears the access token and restores the standard Backbone.sync
.
- Backbone
- JavaScript
btoa()
function (a polyfill is available ifbtoa()
is not supported in your target browser)
The idea of this plugin is to adhere to the standard HTTP Basic Authentication scheme. There is bound to be a 'basic' way to read the username / password combination in your chosen server-side language.