npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

oauthio-web

v0.6.2

Published

OAuth that just works

Downloads

818

Readme

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:

Popup mode

//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
});

Redirection mode

//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
});

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 :

//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 console
        console.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 the result.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 message 
OAuth.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 console
        console.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 the result.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 name
OAuth.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 console
        console.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 the result.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 name
OAuth.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 console
        console.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 the result.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 picture
OAuth.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 picture
        console.log(response);
    })
    .fail(function (err) {
        //handle error with err
    });
})
.fail(function (err) {
    //handle error with 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) :

//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")
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) {
        //handle error with err
    });
})
.fail(function (err) {
    //handle error with 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 [email protected] 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