question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to check if current connect is still valid or expired?

See original GitHub issue

Hi,

I’m using JSforce Node.js package to interact with salesforce?

Is there anyway to check if current connected session is still valid or expired?

        var connection = new jsforce.Connection({
            loginUrl : LOGINURL
        });
        connection.login(USERNAME, PASSWORD + TOKEN, (err: any, res: any) => {
                if (err) {
                    console.log('Connection Completed With Error: ',  err);
                }
                console.log('Connection Token: ' + connection.accessToken);
                console.log('Connection instanceUrl: ' + connection.instanceUrl);
            });
        });

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:8

github_iconTop GitHub Comments

1reaction
paustintcommented, Jul 26, 2022

@johnoliv - that is surprising, but makes sense.

even without the issue you are having, logging in with username and password should be avoided if possible.

But if that is the best way for your use-case, you should still be able to use oauth2 using the username+password flow as documented here: https://help.salesforce.com/s/articleView?id=sf.remoteaccess_oauth_username_password_flow.htm&type=5 which should work with identity and can use the native token refresh flow while still using username and password.

Note: I am not 100% sure if jsforce supports this login flow natively, but you can make the request yourself to get the token, then initialize jsforce with the oauth2 data. I have a vague memory that I tried it once and jsforce did not work.

You need to create a connected app in your production org or a dev org (any org that does not get deleted)

new jsforce.Connection({
  oauth2: new jsforce.OAuth2({
    loginUrl: 'instance-url',
    clientId: 'client id from connected app',
    redirectUri: 'redirect url from connected app, does not matter what it is since not used in this flow but must be configured',
  }),
  instanceUrl: 'instance-url',
  accessToken: 'obtained from auth flow',
  refreshToken: 'obtained from auth flow',
  maxRequest: 5,
  version: '55.0', // best to set this otherwise default to really old version
});
1reaction
paustintcommented, Aug 20, 2021

AFAIK the only way to know if a connection is valid or not is to try out an API call, at which time the token might be refreshed automatically, if possible, or will fail.

const conn = new jsforce.conn({loginUrl: LOGINURL});
await conn.login(USERNAME, `${PASSWORD}${TOKEN}`);

console.log('Connection Token: ' + connection.accessToken);
console.log('Connection instanceUrl: ' + connection.instanceUrl);

// then later on, run something like this
try {
  const identity = await conn.identity();
  // VALID!
} catch(ex) {
  // NOT VALID!
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Check SSL Certificates and Stay Secure - Keyfactor
Click on the certificate pop-up and check the certificate details such as expiry date and the valid duration.
Read more >
Javascript Check if the Date is already Expired in Token
To check if a given JWT is expired then you can just compare against the current date as a UNIX timestamp: var decodedToken...
Read more >
How To Check SSL Certificates [SSL Validation] - Venafi
Click on Certificate (Valid) in the pop-up; Check the Valid from dates to validate the SSL certificate is current. The displayed information includes...
Read more >
How to Check a Certificate's Expiration Date (Chrome)
How to Check a Certificate's Expiration Date (Chrome) ; 1. Click the padlock · Click the Padlock ; 2. Click on Valid ·...
Read more >
How to check TLS/SSL certificate expiration date from Linux CLI
Explains how to check the TLS/SSL certificate expiration date from Linux or Unix CLI and send an email alert using a simple script....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found