From cbb1e92b9ec84788615796dd46a2ab031cdd710c Mon Sep 17 00:00:00 2001 From: Arnab Chakraborty <11457760+Rocky43007@users.noreply.github.com> Date: Wed, 21 Aug 2024 17:25:39 +0300 Subject: [PATCH] Working Supertokens Callback --- interface/app/$libraryId/Layout/auth.tsx | 5 +++- .../client/account/handlers/windowHandler.ts | 27 +++++++++++-------- interface/hooks/useDeeplinkEventHandler.ts | 9 +++++-- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/interface/app/$libraryId/Layout/auth.tsx b/interface/app/$libraryId/Layout/auth.tsx index 559b12760..2376eebcc 100644 --- a/interface/app/$libraryId/Layout/auth.tsx +++ b/interface/app/$libraryId/Layout/auth.tsx @@ -1,6 +1,7 @@ +/* eslint-disable no-restricted-syntax */ /* eslint-disable react-hooks/exhaustive-deps */ import { useEffect } from 'react'; -import { NavigateFunction, useNavigate } from 'react-router-dom'; +import { NavigateFunction, useNavigate, useSearchParams } from 'react-router-dom'; import { signInAndUp } from 'supertokens-web-js/recipe/thirdparty'; import { toast } from '@sd/ui'; @@ -44,8 +45,10 @@ async function handleGoogleCallback(navigate: NavigateFunction) { export const Component = () => { const navigate = useNavigate(); + const [query] = useSearchParams(); useEffect(() => { + (window.location as any).__TEMP_URL_PARAMS = query; handleGoogleCallback(navigate); }, []); diff --git a/interface/app/$libraryId/settings/client/account/handlers/windowHandler.ts b/interface/app/$libraryId/settings/client/account/handlers/windowHandler.ts index f4d16996d..4fa988732 100644 --- a/interface/app/$libraryId/settings/client/account/handlers/windowHandler.ts +++ b/interface/app/$libraryId/settings/client/account/handlers/windowHandler.ts @@ -1,3 +1,4 @@ +import { useSearchParams } from 'react-router-dom'; import { WindowHandlerInterface } from 'supertokens-website/utils/windowHandler/types'; /** @@ -12,20 +13,24 @@ export default function getWindowHandler(original: WindowHandlerInterface): Wind location: { ...original.location, getSearch: function () { - const currentURL = window.location.href; - const firstQuestionMarkIndex = currentURL.indexOf('?'); + // First try with react-router-dom's useUrlSearchParams + // eslint-disable-next-line no-restricted-syntax - if (firstQuestionMarkIndex !== -1) { - // Return the query string from the url - let queryString = currentURL.substring(firstQuestionMarkIndex); + const params: URLSearchParams | string = (window.location as any).__TEMP_URL_PARAMS ?? ''; + return params.toString(); + // const firstQuestionMarkIndex = currentURL.indexOf('?'); - // Remove any hash - if (queryString.includes('#')) { - queryString = queryString.split('#')[0] ?? ''; - } + // if (firstQuestionMarkIndex !== -1) { + // // Return the query string from the url + // let queryString = currentURL.substring(firstQuestionMarkIndex); - return queryString; - } + // // Remove any hash + // if (queryString.includes('#')) { + // queryString = queryString.split('#')[0] ?? ''; + // } + + // // Return the query string from the url + // } return ''; }, diff --git a/interface/hooks/useDeeplinkEventHandler.ts b/interface/hooks/useDeeplinkEventHandler.ts index 16813b05c..c6c879694 100644 --- a/interface/hooks/useDeeplinkEventHandler.ts +++ b/interface/hooks/useDeeplinkEventHandler.ts @@ -10,8 +10,13 @@ export const useDeeplinkEventHandler = () => { const url = e.detail.url; if (!url) return; - - navigate(url); + // If the URL has search params, we need to navigate to the URL with the search params + const [path, search] = url.split('?'); + if (search) { + navigate({ pathname: path, search }); + } else { + navigate(url); + } }; document.addEventListener('deeplink', handler);