Skip to content

Commit

Permalink
Merge pull request #59 from sparrowapp-dev/feat/added-web-app-redirec…
Browse files Browse the repository at this point in the history
…tion

fix: Added redirection logic for web and desktop app
  • Loading branch information
itsmdasifraza authored Oct 1, 2024
2 parents 99d97ea + 6be7bfe commit a9ac451
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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"
VITE_TERMS_OF_SERVICE="https://example.dev/termsandconditions"
VITE_SPARROW_WEB_URL="http://localhost:1422"
16 changes: 16 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@
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';
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 (source === 'web') {
localStorage.setItem('isUserFromDesktop', 'false');
} else {
localStorage.setItem('isUserFromDesktop', 'true');
}
});
</script>

<Router {url}>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
38 changes: 25 additions & 13 deletions src/pages/Auth/login-page/LoginPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
export let id;
let isEmailTouched = false;
Expand Down Expand Up @@ -53,9 +55,12 @@
loadingMessage: 'Please wait while we sign you in....'
};
let loginLoader = false;
let userFromDesktop = localStorage.getItem('isUserFromDesktop');
</script>

{#if isLogin}
{#if isLogin }
<Redirect
title={redirectRules.title}
description={redirectRules.description}
Expand Down Expand Up @@ -96,19 +101,26 @@
const accessToken = response?.accessToken?.token;
const refreshToken = response?.refreshToken?.token;
const sparrowRedirect = `sparrow://?accessToken=${accessToken}&refreshToken=${refreshToken}&response=${JSON.stringify(response)}&event=login`;
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);
redirectRules.buttonClick = () => {

if(userFromDesktop === "true"){
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.');
Expand Down

0 comments on commit a9ac451

Please sign in to comment.