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

oauth2-client-js

v0.0.15

Published

A client implementation for OAuth2 Implicit Grant

Downloads

420

Readme

OAuth2 Client

Build Status Coverage Status

A library to help you handling OAuth2 access and request tokens. Since it is meant to be used in the browser it only includes the OAuth2 Implicit Grant flow.

Installation

npm i --save oauth2-client-js

Usage

A “provider” manages your tokens and knows how to handle responses from the authorization endpoint. Create a new provider like this:

var OAuth = require('oauth2-client-js');
var google = new OAuth.Provider({
    id: 'google',   // required
    authorization_url: 'https://google.com/auth' // required
});

By default a provider will use the localStorage to save its tokens, but with storage you can pass anything that adheres to the Storage API.

Most of the time you will want to do two things: Request new tokens and check whether there are tokens available.

Requesting tokens

To get a new access token you have to redirect the user to the authorization endpoint. The full URI is constructed like this:

// Create a new request
var request = new OAuth.Request({
    client_id: 'my_client_id',  // required
    redirect_uri: 'http://my.server.com/auth-answer'
});

// Give it to the provider
var uri = google.requestToken(request);

// Later we need to check if the response was expected
// so save the request
google.remember(request);

// Do the redirect
window.location.href = uri;

The auth endpoint will redirect the user back to the redirect_uri and encode its response in the URI fragment. Since your application completely lost its state by now, you may want to pass metadata to the request. It will be stored along with the request and loaded later on. E.g. your current application route could go in there.

To parse the response out of the uri, do it like this:

var response = google.parse(window.location.href);

This will either throw an error (e.g. when the state property doesn’t match both in request and response) or return the response. It will have metadata from the request on it. Access and refresh tokens are now available on the provider.

Refresh tokens

They are not in the RFC spec, but you can use them as well (if your server supports them). To issue a refresh request:

var uri = google.refreshToken();
yourHttpLibrary
    .get(uri)
    .then(function(response) {
        google.handleRefresh(response.body);
        // your tokens are now diamonds
        // ehm, up to date.
    });

Get Tokens

google.getAccessToken() and google.getRefreshToken().

License

Apache 2.0 as stated in the LICENSE.