axios-oauth-client
OAuth 2.0 client utils for axios
Installation
$ npm install --save axios-oauth-client axios-token-interceptor axios
Axios OAuth 2.0 Client
Authorization Code grant
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getAuthorizationCode = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'authorization_code',
client_id: 'foo',
client_secret: 'bar',
redirect_uri: '...',
code: '...',
scope: 'baz',
});
const auth = await getAuthorizationCode();
Owner Credentials grant
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getOwnerCredentials = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'password',
client_id: 'foo',
client_secret: 'bar',
username: 'asdf',
password: 'yuio',
scope: 'baz'
});
const auth = await getOwnerCredentials();
Client Credentials grant
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getClientCredentials = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'client_credentials',
client_id: 'foo',
client_secret: 'bar',
scope: 'baz'
});
const auth = await getClientCredentials();
Refresh Token grant
const axios = require('axios');
const oauth = require('axios-oauth-client');
const getRefreshToken = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'refresh_token',
client_id: 'foo',
client_secret: 'bar',
refresh_token: '...',
scope: 'baz'
});
const auth = await getRefreshToken();
Axios OAuth 2.0 Authentication interceptor
const axios = require('axios');
const oauth = require('axios-oauth-client');
const tokenProvider = require('axios-token-interceptor');
const getOwnerCredentials = oauth.client(axios.create(), {
})
const instance = axios.create();
instance.interceptors.request.use(
oauth.interceptor(tokenProvider, getOwnerCredentials)
);
License
MIT