- Notifications
You must be signed in to change notification settings - Fork195
oauth-io/oauth-js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is the JavaScript SDK forOAuth.io. OAuth.io allows you to integrate100+ providers really easily in your web app, without worrying about each provider's OAuth specific implementation.
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
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');
To connect your user using facebook, 2 methods:
//Using popup (option 1)OAuth.popup('facebook').done(function(result){//use result.access_token in your API request//or use result.get|post|put|del|patch|me methods (see below)}).fail(function(err){//handle error with err});
//Using redirection (option 2)OAuth.redirect('facebook',"callback/url");
In callback url :
OAuth.callback('facebook').done(function(result){//use result.access_token in your API request//or use result.get|post|put|del|patch|me methods (see below)}).fail(function(err){//handle error with err});
You can make requests to the provider's API manually with the access token you got from thepopup
orcallback
methods, or use the request methods stored in theresult
object.
GET Request
To make a GET request, you have to call theresult.get
method like this :
//Let's say the /me endpoint on the provider API returns a JSON object//with the field "name" containing the name "John Doe"OAuth.popup(provider).done(function(result){result.get('/me').done(function(response){//this will display "John Doe" in the consoleconsole.log(response.name);}).fail(function(err){//handle error with err});}).fail(function(err){//handle error with err});
POST Request
To make a POST request, you have to call theresult.post
method like this :
//Let's say the /message endpoint on the provider waits for//a POST request containing the fields "user_id" and "content"//and returns the field "id" containing the id of the sent messageOAuth.popup(provider).done(function(result){result.post('/message',{data:{user_id:93,content:'Hello Mr. 93 !'}}).done(function(response){//this will display the id of the message in the consoleconsole.log(response.id);}).fail(function(err){//handle error with err});}).fail(function(err){//handle error with err});
PUT Request
To make a PUT request, you have to call theresult.post
method like this :
//Let's say the /profile endpoint on the provider waits for//a PUT request to update the authenticated user's profile//containing the field "name" and returns the field "name"//containing the new nameOAuth.popup(provider).done(function(result){result.put('/message',{data:{name:"John Williams Doe III"}}).done(function(response){//this will display the new name in the consoleconsole.log(response.name);}).fail(function(err){//handle error with err});}).fail(function(err){//handle error with err});
PATCH Request
To make a PATCH request, you have to call theresult.patch
method like this :
//Let's say the /profile endpoint on the provider waits for//a PATCH request to update the authenticated user's profile//containing the field "name" and returns the field "name"//containing the new nameOAuth.popup(provider).done(function(result){result.patch('/message',{data:{name:"John Williams Doe III"}}).done(function(response){//this will display the new name in the consoleconsole.log(response.name);}).fail(function(err){//handle error with err});}).fail(function(err){//handle error with err});
DELETE Request
To make a DELETE request, you have to call theresult.del
method like this :
//Let's say the /picture?id=picture_id endpoint on the provider waits for//a DELETE request to delete a picture with the id "84"//and returns true or false depending on the user's rights on the pictureOAuth.popup(provider).done(function(result){result.del('/picture?id=84').done(function(response){//this will display true if the user was authorized to delete//the pictureconsole.log(response);}).fail(function(err){//handle error with err});}).fail(function(err){//handle error with err});
Me() Request
Theme()
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 theme()
feature, do like the following (the example works for Facebook, Github, Twitter and many other providers in this case) :
//provider can be 'facebook', 'twitter', 'github', or any supported//provider that contain the fields 'firstname' and 'lastname'//or an equivalent (e.g. "FirstName" or "first-name")varprovider='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){//handle error with err});}).fail(function(err){//handle error with err});
Filtering the results
You can filter the results of theme()
method by passing an array of fields you need :
//...result.me(['firstname','lastname','email'/*, ...*/])//...
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 :)
To test the SDK, you first need to install the npm modulesjasmine-node
andistanbul
(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 installedistanbul
, you can run the following command to get coverage information :
$ npmtest
The coverage report is generated in thecoverage
folder. You can have a nice HTML render of the report incoverage/lcof-report/index.html
This SDK is published under the Apache2 License.
More information inoauth.io documentation
About
OAuth that just works ! This is the JavaScript SDK for OAuth.io
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors8
Uh oh!
There was an error while loading.Please reload this page.