From 288233dfd290ae723b3c89cc64a4d1297b5f9ab3 Mon Sep 17 00:00:00 2001 From: Abhishek Yadav Date: Tue, 1 Oct 2024 18:09:20 +0530 Subject: [PATCH 1/3] fix: Added redirection logic for web and desktop app --- src/App.svelte | 28 +++++++++++++ src/lib/utils/constants.ts | 3 +- src/pages/Auth/login-page/LoginPage.svelte | 47 ++++++++++++++++------ src/store/store.js | 4 ++ 4 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 src/store/store.js diff --git a/src/App.svelte b/src/App.svelte index 816e543..bae4692 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -16,7 +16,35 @@ import ExternalNavigation from './routing/ExternalNavigation.svelte'; import constants from '$lib/utils/constants'; import VerifyEmail from './pages/Auth/verify-email/VerifyEmail.svelte'; + import { onMount } from 'svelte'; + import { isUserFromDesktop } from './store/store'; export let url = '/'; + + + + onMount(() => { + // Check the query parameters in the URL + const urlParams = new URLSearchParams(window.location.search); + const source = urlParams.get('source'); // Get 'source' from query param + + // If a source is found in the query parameters, save it in localStorage + if (source === 'web') { + localStorage.setItem('isUserFromDesktop', 'false'); // Save to localStorage + isUserFromDesktop.set(false); // Update Svelte store + } else if (source === 'desktop') { + localStorage.setItem('isUserFromDesktop', 'true'); // Save to localStorage + isUserFromDesktop.set(true); // Update Svelte store + } else { + // If no source in URL, try to get it from localStorage + const storedSource = localStorage.getItem('isUserFromDesktop'); + if (storedSource === 'true') { + isUserFromDesktop.set(true); + } else { + isUserFromDesktop.set(false); + } + } + }); + diff --git a/src/lib/utils/constants.ts b/src/lib/utils/constants.ts index 609eee0..4cc98e4 100644 --- a/src/lib/utils/constants.ts +++ b/src/lib/utils/constants.ts @@ -11,7 +11,8 @@ const constants = { API_REDIRECT_TIMEOUT: 7000, SPARROW_OAUTH:import.meta.env.VITE_SPARROW_OAUTH, SPARROW_SUPPORT_EMAIL:import.meta.env.VITE_SPARROW_SUPPORT_EMAIL, - SPARROW_TERMS_OF_SERVICE: import.meta.env.VITE_TERMS_OF_SERVICE + SPARROW_TERMS_OF_SERVICE: import.meta.env.VITE_TERMS_OF_SERVICE, + SPARROW_WEB_URL: import.meta.env.VITE_SPARROW_WEB_URL }; export default constants; diff --git a/src/pages/Auth/login-page/LoginPage.svelte b/src/pages/Auth/login-page/LoginPage.svelte index 74df352..c2139bf 100644 --- a/src/pages/Auth/login-page/LoginPage.svelte +++ b/src/pages/Auth/login-page/LoginPage.svelte @@ -15,6 +15,8 @@ import { notifications } from '$lib/components/toast-notification/ToastNotification'; import Button from '$lib/components/button/Button.svelte'; import BgContainer from '$lib/components/bgContainer/BgContainer.svelte'; + import { isUserFromDesktop } from '../../../store/store'; + export let id; let isEmailTouched = false; @@ -53,9 +55,21 @@ loadingMessage: 'Please wait while we sign you in....' }; let loginLoader = false; + + let userFromDesktop + + + + // Subscribe to the store + isUserFromDesktop.subscribe(value => { + userFromDesktop = value; // Update local variable when the store value changes + console.log("IS user from desktop ", isUserFromDesktop) + }); + + -{#if isLogin} +{#if isLogin } { - let data = JSON.parse(window.atob(accessToken?.split('.')[1])); - redirectRules.title = `Welcome back ${data.name}`; - redirectRules.description = `Redirecting you to desktop app...`; - redirectRules.message = `If the application does not open automatically, - please click below.`; - redirectRules.loadingMessage = ''; - redirectRules.isSpinner = false; - navigate(sparrowRedirect); - redirectRules.buttonClick = () => { + + if(userFromDesktop){ + setTimeout(() => { + let data = JSON.parse(window.atob(accessToken?.split('.')[1])); + redirectRules.title = `Welcome back ${data.name}`; + redirectRules.description = `Redirecting you to desktop app...`; + redirectRules.message = `If the application does not open automatically, + please click below.`; + redirectRules.loadingMessage = ''; + redirectRules.isSpinner = false; navigate(sparrowRedirect); - }; - }, 1000); + redirectRules.buttonClick = () => { + navigate(sparrowRedirect); + }; + }, 1000); + + } + else{ + navigate(constants.SPARROW_WEB_URL); + } } else { localStorage.setItem(`timer-verify-${loginCredentials.email}`, new Date().getTime()); notifications.success('Verification code has been sent to your registered Email ID.'); diff --git a/src/store/store.js b/src/store/store.js new file mode 100644 index 0000000..3d8c3cb --- /dev/null +++ b/src/store/store.js @@ -0,0 +1,4 @@ +import { writable } from 'svelte/store'; + +// Initial value is false (assume web), but it will be updated on page load. +export const isUserFromDesktop = writable(false); \ No newline at end of file From eff722f8c62f3126f92bff369fe274f54057aadc Mon Sep 17 00:00:00 2001 From: Abhishek Yadav Date: Tue, 1 Oct 2024 18:22:23 +0530 Subject: [PATCH 2/3] fix: Removed store and used local storage only for storing params --- .env.example | 3 ++- src/App.svelte | 22 +++++----------------- src/pages/Auth/login-page/LoginPage.svelte | 15 ++++----------- src/store/store.js | 4 ---- 4 files changed, 11 insertions(+), 33 deletions(-) delete mode 100644 src/store/store.js diff --git a/.env.example b/.env.example index ddafbbd..25e5501 100644 --- a/.env.example +++ b/.env.example @@ -3,4 +3,5 @@ VITE_SPARROW_SUPPORT_EMAIL="support@example.dev" VITE_SPARROW_OAUTH="http://localhost:9000/api/auth/google/callback" VITE_ENABLE_MIX_PANEL="false" VITE_MIX_PANEL_TOKEN="" -VITE_TERMS_OF_SERVICE="https://example.dev/termsandconditions" \ No newline at end of file +VITE_TERMS_OF_SERVICE="https://example.dev/termsandconditions" +VITE_SPARROW_WEB_URL="http://localhost:1422" diff --git a/src/App.svelte b/src/App.svelte index bae4692..6adde32 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -17,7 +17,6 @@ import constants from '$lib/utils/constants'; import VerifyEmail from './pages/Auth/verify-email/VerifyEmail.svelte'; import { onMount } from 'svelte'; - import { isUserFromDesktop } from './store/store'; export let url = '/'; @@ -26,23 +25,12 @@ // Check the query parameters in the URL const urlParams = new URLSearchParams(window.location.search); const source = urlParams.get('source'); // Get 'source' from query param - - // If a source is found in the query parameters, save it in localStorage if (source === 'web') { - localStorage.setItem('isUserFromDesktop', 'false'); // Save to localStorage - isUserFromDesktop.set(false); // Update Svelte store - } else if (source === 'desktop') { - localStorage.setItem('isUserFromDesktop', 'true'); // Save to localStorage - isUserFromDesktop.set(true); // Update Svelte store - } else { - // If no source in URL, try to get it from localStorage - const storedSource = localStorage.getItem('isUserFromDesktop'); - if (storedSource === 'true') { - isUserFromDesktop.set(true); - } else { - isUserFromDesktop.set(false); - } - } + localStorage.setItem('isUserFromDesktop', 'false'); + + } else { + localStorage.setItem('isUserFromDesktop', 'true'); + } }); diff --git a/src/pages/Auth/login-page/LoginPage.svelte b/src/pages/Auth/login-page/LoginPage.svelte index c2139bf..c555da6 100644 --- a/src/pages/Auth/login-page/LoginPage.svelte +++ b/src/pages/Auth/login-page/LoginPage.svelte @@ -15,7 +15,7 @@ import { notifications } from '$lib/components/toast-notification/ToastNotification'; import Button from '$lib/components/button/Button.svelte'; import BgContainer from '$lib/components/bgContainer/BgContainer.svelte'; - import { isUserFromDesktop } from '../../../store/store'; + export let id; @@ -56,16 +56,9 @@ }; let loginLoader = false; - let userFromDesktop - - - - // Subscribe to the store - isUserFromDesktop.subscribe(value => { - userFromDesktop = value; // Update local variable when the store value changes - console.log("IS user from desktop ", isUserFromDesktop) - }); + let userFromDesktop = localStorage.getItem('isUserFromDesktop'); + console.log("is user form desk ",userFromDesktop) @@ -111,7 +104,7 @@ const refreshToken = response?.refreshToken?.token; const sparrowRedirect = `sparrow://?accessToken=${accessToken}&refreshToken=${refreshToken}&response=${JSON.stringify(response)}&event=login`; - if(userFromDesktop){ + if(userFromDesktop === "true"){ setTimeout(() => { let data = JSON.parse(window.atob(accessToken?.split('.')[1])); redirectRules.title = `Welcome back ${data.name}`; diff --git a/src/store/store.js b/src/store/store.js deleted file mode 100644 index 3d8c3cb..0000000 --- a/src/store/store.js +++ /dev/null @@ -1,4 +0,0 @@ -import { writable } from 'svelte/store'; - -// Initial value is false (assume web), but it will be updated on page load. -export const isUserFromDesktop = writable(false); \ No newline at end of file From 6be7bfef73bdaf33bfa533bc196977e1291d623c Mon Sep 17 00:00:00 2001 From: Abhishek Yadav Date: Tue, 1 Oct 2024 18:38:23 +0530 Subject: [PATCH 3/3] fix: Removed store and used local storage only for storing params --- src/pages/Auth/login-page/LoginPage.svelte | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pages/Auth/login-page/LoginPage.svelte b/src/pages/Auth/login-page/LoginPage.svelte index c555da6..eaa6cda 100644 --- a/src/pages/Auth/login-page/LoginPage.svelte +++ b/src/pages/Auth/login-page/LoginPage.svelte @@ -58,8 +58,6 @@ let userFromDesktop = localStorage.getItem('isUserFromDesktop'); - console.log("is user form desk ",userFromDesktop) - {#if isLogin }