OAuth.io JavaScript SDK
This is the JavaScript SDK for OAuth.io. OAuth.io allows you to integrate 100+ providers really easily in your web app, without worrying about each provider's OAuth specific implementation.
Installation
Getting the SDK
To get the SDK, you can :
- download the zip file from this repository
- get it via Bower or npm (for browserify)
Zip file
Just copy the dist/oauth.js or dist/oauth.min.js to your project.
Bower
$ bower install oauthio-web
npm for browserify
$ npm install oauthio-web
Integrating in your project
In the <head>
of your HTML, include OAuth.js
<script src="/path/to/oauth.js"></script>
In your Javascript, add this line to initialize OAuth.js
OAuth.initialize('your_app_public_key');
Usage
To connect your user using facebook, 2 methods:
OAuth.popup('facebook')
.done(function(result) {
})
.fail(function (err) {
});
Redirection mode
OAuth.redirect('facebook', "callback/url");
In callback url :
OAuth.callback('facebook')
.done(function(result) {
})
.fail(function (err) {
});
Making requests
You can make requests to the provider's API manually with the access token you got from the popup
or callback
methods, or use the request methods stored in the result
object.
GET Request
To make a GET request, you have to call the result.get
method like this :
OAuth.popup(provider)
.done(function(result) {
result.get('/me')
.done(function (response) {
console.log(response.name);
})
.fail(function (err) {
});
})
.fail(function (err) {
});
POST Request
To make a POST request, you have to call the result.post
method like this :
OAuth.popup(provider)
.done(function(result) {
result.post('/message', {
data: {
user_id: 93,
content: 'Hello Mr. 93 !'
}
})
.done(function (response) {
console.log(response.id);
})
.fail(function (err) {
});
})
.fail(function (err) {
});
PUT Request
To make a PUT request, you have to call the result.post
method like this :
OAuth.popup(provider)
.done(function(result) {
result.put('/message', {
data: {
name: "John Williams Doe III"
}
})
.done(function (response) {
console.log(response.name);
})
.fail(function (err) {
});
})
.fail(function (err) {
});
PATCH Request
To make a PATCH request, you have to call the result.patch
method like this :
OAuth.popup(provider)
.done(function(result) {
result.patch('/message', {
data: {
name: "John Williams Doe III"
}
})
.done(function (response) {
console.log(response.name);
})
.fail(function (err) {
});
})
.fail(function (err) {
});
DELETE Request
To make a DELETE request, you have to call the result.del
method like this :
OAuth.popup(provider)
.done(function(result) {
result.del('/picture?id=84')
.done(function (response) {
console.log(response);
})
.fail(function (err) {
});
})
.fail(function (err) {
});
Me() Request
The me()
request is an OAuth.io feature that allows you, when the provider is supported, to retrieve a unified object describing the authenticated user. That can be very useful when you need to login a user via several providers, but don't want to handle a different response each time.
To use the me()
feature, do like the following (the example works for Facebook, Github, Twitter and many other providers in this case) :
var provider = 'facebook';
OAuth.popup(provider)
.done(function(result) {
result.me()
.done(function (response) {
console.log('Firstname: ', response.firstname);
console.log('Lastname: ', response.lastname);
})
.fail(function (err) {
});
})
.fail(function (err) {
});
Filtering the results
You can filter the results of the me()
method by passing an array of fields you need :
result.me(['firstname', 'lastname', 'email'])
Contributing
You are welcome to fork and make pull requests. We will be happy to review them and include them in the code if they bring nice improvements :)
Testing the SDK
To test the SDK, you first need to install the npm modules jasmine-node
and istanbul
(to get the tests coverage) :
$ sudo npm install -g jasmine-node@2.0.0 istanbul
Then you can run the testsuite from the SDK root directory :
$ jasmine-node --verbose tests/unit/spec
Once you've installed istanbul
, you can run the following command to get coverage information :
$ npm test
The coverage report is generated in the coverage
folder. You can have a nice HTML render of the report in coverage/lcof-report/index.html
License
This SDK is published under the Apache2 License.
More information in oauth.io documentation