feat(salesforce): implement refresh token logic
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { IGlobalVariable } from '@automatisch/types';
|
||||
import qs from 'querystring';
|
||||
|
||||
const refreshAccessToken = async ($: IGlobalVariable) => {
|
||||
const searchParams = qs.stringify({
|
||||
grant_type: 'refresh_token',
|
||||
client_id: $.auth.data.consumerKey as string,
|
||||
client_secret: $.auth.data.consumerSecret as string,
|
||||
refresh_token: $.auth.data.refreshToken as string,
|
||||
});
|
||||
const { data } = await $.http.post(
|
||||
`${$.auth.data.oauth2Url}/token?${searchParams}`,
|
||||
{
|
||||
additionalProperties: {
|
||||
shouldRetry: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
await $.auth.set({
|
||||
accessToken: data.access_token,
|
||||
tokenType: data.token_type,
|
||||
idToken: data.id_token,
|
||||
instanceUrl: data.instance_url,
|
||||
signature: data.signature,
|
||||
});
|
||||
};
|
||||
|
||||
export default refreshAccessToken;
|
@@ -20,7 +20,9 @@ const verifyCredentials = async ($: IGlobalVariable) => {
|
||||
|
||||
await $.auth.set({
|
||||
accessToken: data.access_token,
|
||||
refreshToken: data.refresh_token,
|
||||
tokenType: data.token_type,
|
||||
idToken: data.id_token,
|
||||
instanceUrl: data.instance_url,
|
||||
signature: data.signature,
|
||||
userId: data.id,
|
||||
|
@@ -43,7 +43,7 @@ export default function createHttpClient({
|
||||
const { config } = error;
|
||||
const { status } = error.response;
|
||||
|
||||
if (status === 401 && $.app.auth.refreshAccessToken && config.additionalProperties.shouldRetry !== false) {
|
||||
if (status === 401 && $.app.auth.refreshAccessToken && config.additionalProperties?.shouldRetry !== false) {
|
||||
await $.app.auth.refreshAccessToken($);
|
||||
|
||||
// retry the request
|
||||
|
Reference in New Issue
Block a user