Skip to main content

Customer Login

The Spiff Commerce platform provides an addon called the Conversion Accelerator. This addon allows you to retrieve customer details, and use them to improve conversion rates. This page will walk you through the process of authenticating a customer.

Authenticating a Customer​

You'll first want to ensure you've created a client. See Getting Started for more information.

warning

To use the customer authentication features, you will need to specify the applicationKey when creating the client.

Authenticate the customer before creating or loading a Bundle, so that requests for the bundle are made on their behalf.

To associate a customer with a bundle that is already open, refer to Customer Details.

For relaunching a bundle, refer to Returning to an existing bundle.

Verifying a customer exists​

You may want to ensure the customer exists against your partner. You can do this by calling getOrCreateCustomer on the client. This will return a customer object, and a boolean indicating whether the customer is authenticated. A customer is authenticated when a previous login on this device can be resumed: the stored session is reloaded, and refreshed if it has expired. If the customer is authenticated, their details will be assigned internally, so you can skip the rest of this section.

const {
customer, // The customer object. You may not need this, but it's useful for storing the customer's identifier for later use.
isAuthenticated, // Whether the customer is authenticated. This is false for a newly created customer, and for an existing customer whose login session cannot be resumed on this device.
} = await client.getOrCreateCustomer(emailAddress);

Verifying a customer's email address​

If the customer is not authenticated, or you wish to manually re-authenticate, you'll need to get the Spiff Commerce API to send them a verification email. You can do this by calling generateVerificationCode on the client. This will send an email to the customer, with a verification code for them to enter.

// This can raise an error if the input email address is invalid. You'll need to handle this appropriately.
client
.generateVerificationCode(emailAddress)
.then(() => {
// Verification code sent successfully.
})
.catch((error) => {
// Handle the error here.
});

Next, provide a way for the user to input their verification code, then call verifyCode on the client. This will verify the code, and return a boolean indicating whether the customer is authenticated. If the customer is authenticated, their details will be assigned internally.

// This function should only raise an error if the email address is invalid.
// It's fairly safe to assume that this should not happen if you're disabling the input field once they have reached this step.
const isAuthenticated = await client.verifyCode(emailAddress, verificationCode);

Returning to an existing bundle​

Once the customer has been authenticated, reload their bundle by calling getExistingBundle on the client, then call getCurrentStakeholder on the bundle to find out the type of stakeholder the customer is. This will be undefined if the customer has no access to the bundle.

const bundle = await client.getExistingBundle(bundleId);
const stakeholder = await bundle.getCurrentStakeholder();
const stakeholderType = stakeholder?.type;

If the customer is not authenticated, defer to the authentication process and try again.

See the page on Bundles for more information on how to create and manage bundles.

Additional Customer Information​

Once you've authenticated a customer against the core client, you can always get the customer object by calling

// The customer object has general details like id, emailAddress, firstName, lastName, etc.
const customer = client.getCustomer();

Metafields​

Some customers may have additional data stored against them in the form of Metafields.

// Customer must be authenticated for the current session for this to succeed.
const metafields = await client.getCustomerMetafields();
const keyValues = metafields.map((metafield) => ({
value: metafield.value, // The value of the metafield is always a JSON string, e.g. `"text"`, `true`, `1`, etc.
key: metafield.metafieldConfiguration.name,
}));