1

I am trying to build a server that will populate Salesforce through a front end website using Express JS with JSForce library. However, every time the access token expired, it does not refresh by itself. The connection instructor already has refreshToken in it and I already did Oauth2 Flow. Below is the code to establish connection.

const _login = () => {
  let conn = new jsforce.Connection(session);
  conn.on('refresh', (accessToken, result) => {
    console.log('here');
    const newConnection = new jsforce.Connection({
      accessToken,
      instanceUrl: result.instance_url
    });
    process.env.ACCESS_TOKEN = accessToken;
    console.log(accessToken);
    conn = newConnection;
  });
  return conn;
};

Is there anything wrong? I put this function separated from app.js and import this function into a router file. Thank you for your help!

2

You don't need to create a new connection on a refresh. This will cause problems. You can use the handler to store the access token, but you should not be forcing a new connection.

1
  • the problem is, the code does not even pass let conn = new jsforce.Connection(session); since it throws error invalid_grant: expired access/refresh token. so my handler is not fired. – jimmy faza Nov 21 '19 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.