UNPKG

authbase

Version:

AuthBase client library

32 lines (25 loc) 864 B
module.exports = function(appId, remote, readyHandler, messageHandler) { // Create the IFrame we send messages to and append it to the body var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = (remote || 'https://api.authbase.io') + '/messenger?appId=' + appId; document.body.appendChild(iframe); var bus = iframe.contentWindow || iframe.contentDocument; // Message event handler window.addEventListener('message', function(event) { messageHandler(JSON.parse(event.data)); }); // IFrame has loaded, so emit the ready event, and then clear the queue iframe.onload = readyHandler; // Create the send function function send(type, data, form) { bus.postMessage(JSON.stringify({ type: type, payload: data, form: form, }), remote); } return { send: send }; };