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

superagent-oauth2-client

v0.0.17

Published

A superagent plugin for oauth2-client

Downloads

20

Readme

superagent-oauth2-client

Build Status Coverage Status

A superagent plugin for stressless OAuth2 token management using oauth2-client.

Installation

npm i --save superagent-oauth2-client

Usage

var superagent = require('superagent'),
    request = require('superagent-oauth2-client')(superagent),
    OAuth = require('oauth2-client-js');

// define a single oauth provider
// will use localstorage to save tokens
var provider = new OAuth.Provider({
    id: 'google',
    authorization_url: 'https://google.com/auth'
});

request
    .get('http://server.com')
    .oauth(provider, {
        redirect_uri: '...',
        client_id: 'client_id'
    })
    .exec()
    .then()      // business logic
    .catch();    // error handling

then will only be called when the original request was successful, no matter if the first or the second time.

catch may be called for varying reasons. We may have used a valid token, but the server responded with an error. Or we might just be about to redirect the user to the auth endpoint.

How does it work

.oauth() hides almost all the magic from you. What is this magic?

When you do a request it will first look if the provider has an access token. If it does, it will set it on the Authorization header and send the request. If it doesn’t, it will automatically redirect to the authorization_url (ie. window.location.href=xxx).

In the happy case the user logged in, gave its consent and will be redirected to the redirect_uri in your app. There is some manual work to do now, you have to parse the encoded response and react accordingly.

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

It it doesn’t throw an error, everything’s good now.

If there was an access token, but it’s considered invalid (=> server returns 401 status), .oauth will redirect the user to the auth endpoint again.

How to save application state

You can pass a function to exec() that will be passed the oauth access token request that’s about to be issued. There you can set arbitrary things in the metadata field. This will be again available after you the reponse was parsed successfully.

request
    ...
    .exec(function(req) {
        req.metadata.time = Date.now();
        req.metadata.currentRoute = window.location.path;
    })
    .then...

var response = provider.parse(window.location.href);
navigateTo(response.metadata.currentRoute);

License

Apache 2.0 as stated in the LICENSE.