Generate login tokens

Edit on GitHub

The Customer Login by Token feature lets B2B users log in to a Spryker Shop using a token.

A token is a unique identifier that contains all the information needed for authentication to fetch a specific resource without using a username and password. The tokens are JSON strings that are encoded in the base64url format.

To generate a token, follow the steps:

  1. Adjusted the following transfers for expansion:
<transfer name="Customer">
	<property name="additionalProperty" type="array" />
</transfer>

<transfer name="CompanyUserIdentifier">
	<property name="additionalProperty" type="array" />
</transfer>

<transfer name="OauthUser">
	<property name="additionalProperty" type="array" />
</transfer>

<transfer name="OauthRequest">
	<property name="additionalProperty" type="array" />
</transfer>
  1. Generate a token using a facade call OauthCompanyUserFacade::createCompanyUserAccessToken():
$customerTransfer = (new CustomerTransfer())
	->setCompanyUserTransfer((new CompanyUserTransfer())->setIdCompanyUser(11))
	->setAdditionalProperty(['key' => 'value']);

$oauthResponseTransfer = OauthCompanyUserFacade::createCompanyUserAccessToken($customerTransfer);
$accessToken = $oauthResponseTransfer->getAccessToken();
  1. Retrieve the customer by an access token using a client call OauthCompanyUserClient::getCustomerByAccessToken():
$customerTransfer = OauthCompanyUserClient::getCustomerByAccessToken($accessToken)->getCustomerTransfer();

$additionalProperty = $customerTransfer->getAdditionalProperty(); // ['key' => 'value']