#1303: Is it possible to initiate a logout from the account in the widget programmatically?
Отредактирована: 217 дней назадSymptoms
Is it possible to initiate a logout from the account in the widget programmatically?
Is there an API for logging out of the widget account?
Solution
Yes, it is possible to initiate a logout from the widget account programmatically using the standard browser API window.postMessage on the DOM iframe object:
const iframe = document.getElementById("swarmica_widget_iframe_id");
iframe.contentWindow.postMessage({ type: "LOGOUT_WIDGET" }, "*");
For example, this is what the code for logging out of the account when a button is clicked might look like:
<!-- parent HTML -->
<button id="logoutWidgetBtn">
Logout widget
</button>
<!-- start embedded Swarmica widget script -->
<iframe src="..." id="swarmica_widget_iframe_id" style="position: fixed; right: 12px; bottom: 12px; width: 56px; height: 56px; overflow: hidden; "
allowTransparency="true"
frameborder="0"
allowTransparency="true" frameborder="0"></iframe>
<!-- rest of embed code -->
<script>
const iframe = document.getElementById("swarmica_widget_iframe_id")
iframe.onload = () => {
renderWidget("swarmica_widget_iframe_id")
}
document.getElementById("swarmica_widget_iframe_id").addEventListener("click",
() => {
if (!iframe || !iframe.contentWindow) return;
iframe.contentWindow.postMessage({ type: "LOGOUT_WIDGET" }, "*")
});
</script>
<!-- end embedded Swarmica widget scripts -->
You will need to have Swarmica version v5.7.2 or above