A package that makes using the OAuth2 PKCE flow easier
npm install @pnlpal/js-pkcenpm i js-pkce``javascript`
import PKCE from 'js-pkce';
const pkce = new PKCE({
client_id: 'myclientid',
redirect_uri: 'http://localhost:8080/auth',
authorization_endpoint: 'https://authserver.com/oauth/authorize',
token_endpoint: 'https://authserver.com/oauth/token',
requested_scopes: '*',
});
`javascript`
window.location.replace(pkce.authorizeUrl());
You may add additional query parameters to the authorize url by using an optional second parameter:
`javascript`
const additionalParams = {test_param: 'testing'};
window.location.replace(pkce.authorizeUrl(additionalParams));
parameter you set when creating the instance.
Again, this is an example that might work for a SPA.When you get back here, you need to exchange the code for a token.
`javascript
const url = window.location.href;
pkce.exchangeForAccessToken(url).then((resp) => {
const token = resp.access_token;
// Do stuff with the access token.
});
`As with the authorizeUrl method, an optional second parameter may be passed to
the
exchangeForAccessToken method to send additional parameters to the request:`javascript
const url = window.location.href;
const additionalParams = {test_param: 'testing'};pkce.exchangeForAccessToken(url, additionalParams).then((resp) => {
const token = resp.access_token;
// Do stuff with the access token.
});
``