Skip to content

Commit

Permalink
sync with TelosCloudJs - 0.9.19
Browse files Browse the repository at this point in the history
  • Loading branch information
Viterbo committed Apr 23, 2024
1 parent d29ebaf commit af3f496
Showing 1 changed file with 62 additions and 44 deletions.
106 changes: 62 additions & 44 deletions src/boot/telosCloudJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string, any>;

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) {
Expand All @@ -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, '*');
}
},
});

Expand Down

0 comments on commit af3f496

Please sign in to comment.