title | description | ms.date | ms.topic | author | ms.author |
---|---|---|---|---|---|
setAuthTokenProvider (Omnichannel for Customer Service JavaScript API reference) | MicrosoftDocs |
Includes reference information such as syntax and parameter for the setAuthTokenProvider method in Omnichannel for Customer Service JavaScript API reference. Also included is a sample code for using the method. |
04/04/2022 |
reference |
gandhamm |
mgandham |
[!INCLUDEcc-use-with-omnichannel]
[!INCLUDEsetAuthTokenProvider-description]
The authentication token provider function, when called with a callback function as a parameter, invokes the callback function with a valid JSON Web Token (JWT) string as the argument.
This method raises an error if the value of the authTokenProvider
parameter is not a function.
Important
- In the payload of the JWT token, the value provided for
lwicontexts
key should be the serialized custom context. - The custom context is a collection of key/value pairs. Only primitive values are allowed for any key.
- The keys of custom context must correspond to context variables that are created for the associated work stream in Omnichannel for Customer Service.
- The authentication token provider would be invoked by live chat widget when starting a new chat.
Note
The live chat SDK methods should be invoked after the lcw:ready event is raised. You can listen for this event by adding your own event listener on the window object.
This SDK is applicable only if authentication is enabled for the widget. To enable authentication for your widget, see Create chat authentication settings.
Microsoft.Omnichannel.LiveChatWidget.SDK.setAuthTokenProvider(authTokenProvider);
Parameter | Type | Description |
---|---|---|
authTokenProvider | Function | Function which when invoked with a callback function as an argument, fetches the JWT token and invokes the callback function with the JWT token as its argument |
None
{
"sub" : "87b4d06c-abc2-e811-a9b0-000d3a10e09e",
"lwicontexts" :"{\"msdyn_cartvalue\":\"10000\", \"msdyn_isvip\":\"false\"}",
"iat" : 1542622071,
"iss" : "contosohelp.com",
"exp" : 1542625672,
"nbf" : 1542622072
}
Note
In the above payload, the value corresponding to lwicontexts
key should be the serialized custom context.
The keys msdyn_cartvalue
and msdyn_isvip
in the serialized custom context correspond to context variables that are created for the associated work stream in Omnichannel for Customer Service.
let payloadToEncrypt = {
"sub" : "87b4d06c-abc2-e811-a9b0-000d3a10e09e",
"lwicontexts" :"{\"msdyn_cartvalue\":\"10000\", \"msdyn_isvip\":\"false\"}",
"iat" : 1542622071,
"iss" : "contosohelp.com",
"exp" : 1542625672,
"nbf" : 1542622072
};
function convertToJwtToken(payloadToEncrypt){
// Ideally, you call your service to convert the payload to a valid JWT token
return Promise.resolve("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI4N2I0ZDA2Yy1hYmMyLWU4MTEtYTliMC0wMDBkM2ExMGUwOWUiLCJsd2ljb250ZXh0cyI6IntcIm1zZHluX2NhcnR2YWx1ZVwiOlwiMTAwMDBcIiwgXCJtc2R5bl9pc3ZpcFwiOlwiZmFsc2VcIn0iLCJpYXQiOjE1NDI2MjIwNzEsImlzcyI6ImNvbnRvc29oZWxwLmNvbSIsImV4cCI6MTU0MjYyNTY3MiwibmJmIjoxNTQyNjIyMDcyfQ.r37z1M5rMyRYMOJ-rhyTRYFOgvl9N7KvTMueSFPkiuM");
}
window.addEventListener("lcw:ready", function handleLivechatReadyEvent(){
// Sets the auth-token provider
// Throws error if authTokenProvider is not a function
convertToJwtToken(payloadToEncrypt).then(function (jwtToken){
Microsoft.Omnichannel.LiveChatWidget.SDK.setAuthTokenProvider(function authTokenProvider(callback){
callback(jwtToken);
});
});
});
The following errors can occur with this method.
Error code | Error message |
---|---|
2 | Auth token provider method execution failed |
3 | Auth token provider didn't provide any token |
4 | Auth token provider provided invalid token |
JavaScript API reference for live chat SDK
[!INCLUDEfooter-include]