Setup
Custom Identity Provider is essentially a singleton function.window.__lc object (in case of more advanced applications). However, it has to be provided before widget initialization.
identityProvider parameter of the configuration object.
Methods
In order to make Custom Identity Provider work, you’ll have to properly implement and provide a set of following methods:getToken- resolving Chat Widget token. If you want to cache the token, this should return the cached token instead of a fresh request tohttps://accounts.livechat.com/customer/tokenendpoint.getFreshToken- resolving Chat Widget token. This should always make a call for a fresh token fromhttps://accounts.livechat.com/customer/tokenendpoint.hasToken- resolving boolean. It determines whether a token has been acquired.invalidate- resolving nothing. When called, it should remove the current token. There is no need to do anything else as a new token will be requested bygetFreshTokenafterward.
window.__lc.custom_identity_provider.
Chat Widget token
The authorization token to resolve in functions should contain the following properties:How to acquire it?
In order to get a Chat Widget token, you need to implement a function that calls thehttps://accounts.livechat.com/customer/token endpoint. To do so, you can follow customer authorization flows or have a look at our example that uses cookies.
The first time you execute this logic, the connection between our customer_id (entity_id) and the identity from your service doesn’t exist yet. Start by creating a new custom identity. Next time you execute this logic, you’ll be able to provide entity_id, and therefore, you will only need to refresh the token.
Mind the fact that the data returned from the endpoint is not 1:1 with the token shape described above. You’ll have to make the following adjustments to your token before passing it further:
- Rename all the keys from
snake_casetocamelCase - Add the
creationDateandlicenseIdfields. - Multiply
expiresInby 1000. It’s because we expectexpiresInto be expressed in milliseconds while the endpoint operates in seconds.
Examples
Here are our suggested implementations of Custom Identity Provider. ThefetchTextToken() method is used in the examples to acquire the Chat Widget token.
Without caching
If you don’t need to cache the token, the fetching logic shall occur on every refresh of your application page. In such a case, the implementation can be very simple – we only have to ensure the handling of thegetToken promise, and resolve other promises.