From a71d42252826041d45cd093bb5239694c21537bd Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Thu, 20 Apr 2023 16:13:22 +0300 Subject: [PATCH] fix(refreshTokensForViya): fixed FormData logic --- src/auth/getTokens.ts | 3 +++ src/auth/refreshTokensForViya.ts | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/auth/getTokens.ts b/src/auth/getTokens.ts index 9dc0c1c..340331a 100644 --- a/src/auth/getTokens.ts +++ b/src/auth/getTokens.ts @@ -29,9 +29,12 @@ export async function getTokens( const error = 'Unable to obtain new access token. Your refresh token has expired.' logger.error(error) + throw new Error(error) } + logger.info('Refreshing access and refresh tokens.') + const tokens = serverType === ServerType.SasViya ? await refreshTokensForViya( diff --git a/src/auth/refreshTokensForViya.ts b/src/auth/refreshTokensForViya.ts index e10d15c..ecb2c4d 100644 --- a/src/auth/refreshTokensForViya.ts +++ b/src/auth/refreshTokensForViya.ts @@ -2,6 +2,7 @@ import { SasAuthResponse } from '@sasjs/utils/types' import { prefixMessage } from '@sasjs/utils/error' import * as NodeFormData from 'form-data' import { RequestClient } from '../request/RequestClient' +import { isNode } from '../utils' /** * Exchanges the refresh token for an access token for the given client. @@ -17,8 +18,7 @@ export async function refreshTokensForViya( refreshToken: string ) { const url = '/SASLogon/oauth/token' - let token - token = + const token = typeof Buffer === 'undefined' ? btoa(clientId + ':' + clientSecret) : Buffer.from(clientId + ':' + clientSecret).toString('base64') @@ -27,8 +27,7 @@ export async function refreshTokensForViya( Authorization: 'Basic ' + token } - const formData = - typeof FormData === 'undefined' ? new NodeFormData() : new FormData() + const formData = isNode() ? new NodeFormData() : new FormData() formData.append('grant_type', 'refresh_token') formData.append('refresh_token', refreshToken)