Skip to content

Authentication

Charlie Jonas edited this page Nov 15, 2018 · 5 revisions

Before we can use our generated classes, we must first authenticate a connection to salesforce.

  1. Create/open a new file ./src/index.ts
  2. Add these imports:
import * as child_process from 'child_process'
import {setDefaultConfig} from 'ts-force'
import { Account, Contact } from './generated'
  1. Add the following code:
// MAKE SURE TO UPDATE 'SET_THIS' to your dev org user
let user = 'SET_THIS'
let orgInfo: {result: {accessToken: string, instanceUrl: string}} = JSON.parse(
    child_process.execSync(`sfdx force:org:display -u ${user} --json`
).toString('utf8'));

setDefaultConfig({
    accessToken: orgInfo.result.accessToken,
    instanceUrl:  orgInfo.result.instanceUrl,
});

The above snippet uses sfdx-cli to get the accessToken & instanceUrl for your dev org user. In a real app, you would typically get these values from oAuth or Visualforce getSessionId.

The configuration is passes it to setDefaultConfig, which creates a connection which will be used by default. (see readme for using with multiple connections).

Clone this wiki locally