(function () { // const issuerId = '314ebacf' const apiUrl = 'https://laestancia.dashboard.securitize.io/api/v1' const dashboardUrl = 'https://laestancia.dashboard.securitize.io' const isRestricted = 'true' const url = new URL(window.location.href) const referralId = url.searchParams.get('referral_id') if (referralId) window.history.pushState('', document.title, '/') const getUuidv4 = () => { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } const getCookie = (name) => { let matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" )) return matches ? decodeURIComponent(matches[1]) : '' } const state = { uniqueToken: localStorage.getItem('uniqueToken'), dashboardUrl: dashboardUrl, } const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } const getDashboardUrl = async () => { const rawResponse = await fetch(`${apiUrl}/integrations/dashboard_url`, { method: 'GET', headers, }) const { dashboardUrl } = await rawResponse.json() if (!dashboardUrl) { console.log('Error getting dashboard url') return } localStorage.setItem('dashboardUrl', dashboardUrl) state.dashboardUrl = dashboardUrl } const getUserToken = async () => { const uniqueToken = getUuidv4(); localStorage.setItem('uniqueToken', uniqueToken) state.uniqueToken = uniqueToken } const setUserNotLogged = async () => { const securitizeState = document.querySelectorAll('[data-securitize-state="logged-in"]') for (const element of securitizeState) { element.style.display = 'none' } const loggedOutActionElements = document.querySelectorAll('[data-securitize-logged-out-action]') for (const element of loggedOutActionElements) { element.textContent = element.dataset['securitizeLoggedOutText'] element.dataset['securitizeAction'] = element.dataset['securitizeLoggedOutAction'] } const mainActionElements = document.querySelectorAll('[data-securitize-action="main"]') for (const element of mainActionElements) { element.addEventListener('click', e => { e.preventDefault() window.location = `${state.dashboardUrl}/registration?uniqueToken=${state.uniqueToken || getUuidv4()}${referralId ? `&referral_id=${referralId}` : ''}` }, false) } const loginActionElements = document.querySelectorAll('[data-securitize-action="login"]') for (const element of loginActionElements) { element.addEventListener('click', e => { e.preventDefault() window.location = `${state.dashboardUrl}/login?uniqueToken=${state.uniqueToken}` }, false) } } const getQueryString = (params) => { return Object.keys(params).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key])).join('&') } const checkIsLoggedIn = async () => { const rawResponse = await fetch(`${apiUrl}/session?${getQueryString({ uniqueToken: state.uniqueToken })}`, { method: 'GET', headers, }) const response = await rawResponse.json() return response.loggedIn } const initNotLogged = async () => { await getUserToken() await setUserNotLogged() } const setUserLogged = async () => { const isLoggedIn = await checkIsLoggedIn() if (!isLoggedIn) return setUserNotLogged() const securitizeState = document.querySelectorAll('[data-securitize-state="logged-out"]') for (const element of securitizeState) { element.style.display = 'none' } const loggedInActionElements = document.querySelectorAll('[data-securitize-logged-in-action]') for (const element of loggedInActionElements) { element.textContent = element.dataset['securitizeLoggedInText'] element.dataset['securitizeAction'] = element.dataset['securitizeLoggedInAction'] } const dashboardActionElements = document.querySelectorAll('[data-securitize-action="dashboard"]') for (const element of dashboardActionElements) { element.addEventListener('click', e => { e.preventDefault() window.location = `${state.dashboardUrl}?uniqueToken=${state.uniqueToken || getUuidv4()}` }, false) } const logoutActionElements = document.querySelectorAll('[data-securitize-action="logout"]') for (const element of logoutActionElements) { element.addEventListener('click', e => { e.preventDefault() const accessToken = getCookie('accessToken') let newHeaders = Object.assign({ authorization: `Bearer ${accessToken}` }, headers) fetch(`${apiUrl}/session`, { method: 'DELETE', headers: newHeaders, }).finally(() => { localStorage.removeItem('uniqueToken') window.location.reload(true) }) }, false) } } const setUserRestrictions = () => { let restrictedState; restrictedState = isRestricted ? document.querySelectorAll('[data-securitize-restriction-state="restricted"]') : document.querySelectorAll('[data-securitize-restriction-state="unrestricted"]') for (const element of restrictedState) { element.style.display = 'none' } } document.addEventListener('DOMContentLoaded', async event => { // if (!state.dashboardUrl) await getDashboardUrl() setUserRestrictions() if (!state.dashboardUrl) return if (!state.uniqueToken) return await initNotLogged() await setUserLogged() }) })()