
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
oauth2-client-js
Advanced tools
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.
npm i --save oauth2-client-js
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.
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.
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.
});
google.getAccessToken()
and google.getRefreshToken()
.
Apache 2.0 as stated in the LICENSE.
FAQs
A client implementation for OAuth2 Implicit Grant
The npm package oauth2-client-js receives a total of 13 weekly downloads. As such, oauth2-client-js popularity was classified as not popular.
We found that oauth2-client-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.