Working Supertokens Callback

This commit is contained in:
Arnab Chakraborty
2024-08-21 17:25:39 +03:00
parent 6ef5e36cf2
commit cbb1e92b9e
3 changed files with 27 additions and 14 deletions

View File

@@ -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);
}, []);

View File

@@ -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 '';
},

View File

@@ -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);