@@ -5,28 +5,36 @@ import { Logger } from "@/utils/types";
55const createSalesforceAuth = ( opts : ConnectionOptions , logger : Logger ) => {
66 return {
77 async authenticate ( ) : Promise < Connection > {
8- return new Promise < Connection > ( async ( resolve , reject ) => {
9- logger . info ( "Authenticating/Reauthenticating to Salesforce" ) ;
10- const client = new Connection ( opts ) ;
11-
12- const data = await client . oauth2 . refreshToken ( opts . refreshToken , ( err , res ) => {
13- if ( err ) {
14- logger . error ( { message : "Error authenticating to Salesforce" } ) ;
15- reject ( err ) ;
16- }
8+ logger . info ( "Authenticating/Reauthenticating to Salesforce" ) ;
9+
10+ // Create the initial connection using full credentials.
11+ // Make sure opts includes refreshToken, clientId, and clientSecret.
12+ const client = new Connection ( opts ) ;
13+
14+ try {
15+ // Wrap the callback-based refreshToken method into a Promise.
16+ const data = await new Promise < any > ( ( resolve , reject ) => {
17+ client . oauth2 . refreshToken ( opts . refreshToken , ( err , res ) => {
18+ if ( err ) {
19+ logger . error ( { message : "Error refreshing token" , err } ) ;
20+ return reject ( err ) ;
21+ }
22+ resolve ( res ) ;
23+ } ) ;
1724 } ) ;
1825
19- logger . debug ( `Salesforce OAuth2 Refreshed: ${ JSON . stringify ( data , null , 2 ) } ` ) ;
20-
21- const newClient = new Connection ( {
22- accessToken : data . access_token ,
23- instanceUrl : data [ "instance_url" ] ,
24- } ) ;
26+ // Instead of creating a new connection, update the existing connection.
27+ client . accessToken = data . access_token ;
28+ client . instanceUrl = data . instance_url ;
2529
30+ logger . debug ( `Salesforce OAuth2 Refreshed: ${ JSON . stringify ( data , null , 2 ) } ` ) ;
2631 logger . info ( "Authentication/Reauthentication Successful" ) ;
2732
28- resolve ( newClient ) ;
29- } ) ;
33+ return client ;
34+ } catch ( err ) {
35+ logger . error ( { message : "Error authenticating to Salesforce" , err } ) ;
36+ throw err ;
37+ }
3038 } ,
3139 } ;
3240} ;
0 commit comments