Skip to content

@salesforce/b2c-tooling-sdk / auth / resolveAuthStrategy

Function: resolveAuthStrategy()

resolveAuthStrategy(credentials, options): AuthStrategy

Defined in: packages/b2c-tooling-sdk/src/auth/resolve.ts:160

Resolves and creates the appropriate auth strategy based on credentials and allowed methods.

Iterates through allowed methods in priority order and returns the first strategy for which the required credentials are available.

Parameters

credentials

AuthCredentials

The available credentials

options

ResolveAuthStrategyOptions = {}

Resolution options (allowed methods, etc.)

Returns

AuthStrategy

The resolved auth strategy

Throws

Error if no allowed method has the required credentials

Example

typescript
import { resolveAuthStrategy } from '@salesforce/b2c-tooling-sdk';

// Will use client-credentials if secret is available, otherwise implicit
const strategy = resolveAuthStrategy({
  clientId: 'my-client-id',
  clientSecret: process.env.CLIENT_SECRET, // may be undefined
  scopes: ['sfcc.products'],
});

// Force implicit auth only
const implicitStrategy = resolveAuthStrategy(
  { clientId: 'my-client-id' },
  { allowedMethods: ['implicit'] }
);

// Use the strategy
const response = await strategy.fetch('https://example.com/api');

All rights reserved.