From af3f496a189c0a78aebc78c815e18dd10f12eea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viterbo=20Rodr=C3=ADguez?= Date: Tue, 23 Apr 2024 01:31:41 -0300 Subject: [PATCH] sync with TelosCloudJs - 0.9.19 --- src/boot/telosCloudJs.ts | 106 +++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 44 deletions(-) diff --git a/src/boot/telosCloudJs.ts b/src/boot/telosCloudJs.ts index dfb82a73..c2de68cb 100644 --- a/src/boot/telosCloudJs.ts +++ b/src/boot/telosCloudJs.ts @@ -2,6 +2,7 @@ // if redirect or iframe is set, it means the user is coming from an external source // and wants to use the wallet to login and come back to the external source import { + ComponentCustomProperties, ref, } from 'vue'; import { boot } from 'quasar/wrappers'; @@ -13,18 +14,74 @@ import { import { MetakeepAuthenticator } from 'src/antelope/wallets/ual/MetakeepUAL'; import { createTraceFunction } from 'src/antelope/config'; -export const redirectParam = new URLSearchParams(window.location.search).get('redirect'); -export const iframeParam = new URLSearchParams(window.location.search).get('iframe'); +const url = new URLSearchParams(window.location.search); +export const redirectParam = url.get('redirect'); +export const iframeParam = url.get('iframe'); +export const logoutParam = url.get('logout'); export const redirect = ref<{url:string, hostname:string} | null>(null); export const redirectShow = ref(false); export const iframeShow = ref(false); -export default boot(async ({ app }) => { +const trace = createTraceFunction('telosCloudJs'); + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +let globalProps = {} as ComponentCustomProperties & Record; - const trace = createTraceFunction('telosCloudJs'); +export const telosCloudResponse = async () => { const ant = getAntelope(); const accountStore = useAccountStore(); + trace('ant.events.onLoggedIn().....'); + // if the redirect parameter is present, show a confirm notification to the user + + if (redirectShow.value && redirect.value) { + const hostname = redirect.value.hostname; + const message = globalProps.$t('home.redirect_notification_message', { hostname }); + globalProps.$notifyWarningWithAction(message, { + label: ant.config.localizationHandler('home.redirect_me'), + handler: () => { + // we redirect the user to the url + if (redirect.value) { + // we need to generate a new url based on redirect.value?.url adding the accountStore.loggedNativeAccount?.account + // and the email of the user if it's a metakeep authenticator + const url = new URL(redirect.value.url); + trace('onLoggedIn', 'url', url.toString()); + url.searchParams.set('account', accountStore.loggedNativeAccount?.account || ''); + trace('onLoggedIn', 'adding the account...', url.toString()); + const authenticator = accountStore.loggedNativeAccount.authenticator; + trace('onLoggedIn', 'adding the email...', url.toString()); + const auth = authenticator as never as MetakeepAuthenticator; + url.searchParams.set('email', auth.getEmail()); + trace('onLoggedIn', 'redirecting to', url.toString()); + window.location.href = url.toString(); + } + }, + }); + } else if (iframeShow.value) { + // if the iframe parameter is present, we send the credentials to the parent window + const authenticator = accountStore.loggedNativeAccount.authenticator; + const auth = authenticator as never as MetakeepAuthenticator; + const credentials: {account: string, email: string, keys: string[] } = { + account: accountStore.loggedNativeAccount?.account || '', + email: auth.getEmail(), + keys: auth.getKeys() ?? [], + }; + const str = JSON.stringify(credentials); + trace('onLoggedIn', 'credentials', credentials, str, 'logoutParam:', logoutParam); + trace('--------------------------------------------------------------------------------'); + trace(window.location.href); + trace('--------------------------------------------------------------------------------'); + if (logoutParam) { + accountStore.logout(); + } + window.parent.postMessage(str, '*'); + } +}; + +export default boot(async ({ app }) => { + const ant = getAntelope(); + globalProps = app.config.globalProperties; + if (redirectParam) { const isValid = new RegExp('^(http|https)://', 'i').test(redirectParam); if (isValid) { @@ -49,47 +106,8 @@ export default boot(async ({ app }) => { const subscription = ant.events.onLoggedIn.subscribe({ next: () => { + telosCloudResponse(); subscription.unsubscribe(); - const globalProps = app.config.globalProperties; - // if the redirect parameter is present, show a confirm notification to the user - - if (redirectShow.value && redirect.value) { - const hostname = redirect.value.hostname; - const message = globalProps.$t('home.redirect_notification_message', { hostname }); - globalProps.$notifyWarningWithAction(message, { - label: ant.config.localizationHandler('home.redirect_me'), - handler: () => { - // we redirect the user to the url - if (redirect.value) { - // we need to generate a new url based on redirect.value?.url adding the accountStore.loggedNativeAccount?.account - // and the email of the user if it's a metakeep authenticator - const url = new URL(redirect.value.url); - trace('onLoggedIn', 'url', url.toString()); - url.searchParams.set('account', accountStore.loggedNativeAccount?.account || ''); - trace('onLoggedIn', 'adding the account...', url.toString()); - const authenticator = accountStore.loggedNativeAccount.authenticator; - trace('onLoggedIn', 'adding the email...', url.toString()); - const auth = authenticator as never as MetakeepAuthenticator; - url.searchParams.set('email', auth.getEmail()); - trace('onLoggedIn', 'redirecting to', url.toString()); - window.location.href = url.toString(); - } - }, - }); - } else if (iframeShow.value) { - // if the iframe parameter is present, we send the credentials to the parent window - const authenticator = accountStore.loggedNativeAccount.authenticator; - const auth = authenticator as never as MetakeepAuthenticator; - const credentials: {account: string, email: string, keys: string[] } = { - account: accountStore.loggedNativeAccount?.account || '', - email: auth.getEmail(), - keys: auth.getKeys() ?? [], - }; - const str = JSON.stringify(credentials); - trace('onLoggedIn', 'credentials', credentials, str); - accountStore.logout(); - window.parent.postMessage(str, '*'); - } }, });