diff --git a/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx b/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx index 03016ae..27bb514 100644 --- a/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx +++ b/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx @@ -1,4 +1,10 @@ -import { PropsWithChildren, useContext, useMemo, useState } from 'react'; +import { + PropsWithChildren, + useContext, + useMemo, + useState, + useRef, +} from 'react'; import { CookieAdapter, SDKCore } from '@fusionauth-sdk/core'; @@ -12,32 +18,25 @@ import { import { FusionAuthContext, UserInfo as DefaultUserInfo } from './Context'; import { FusionAuthProviderContext } from './FusionAuthProviderContext'; -let core: SDKCore; - -function getNewCore( - config: FusionAuthProviderConfig, - onTokenExpiration: () => void, - cookieAdapter?: CookieAdapter, -) { - if (core) { - core.dispose(); - } - core = new SDKCore({ - ...config, - onTokenExpiration, - cookieAdapter, - }); - return core; -} - function FusionAuthProvider({ children, ...config }: PropsWithChildren & FusionAuthProviderConfig) { const cookieAdapter = useCookieAdapter(config); + const coreRef = useRef(); const core: SDKCore = useMemo(() => { - return getNewCore(config, () => setIsLoggedIn(false), cookieAdapter); + if (coreRef.current) { + coreRef.current.dispose(); + } + + const newCore = new SDKCore({ + ...config, + cookieAdapter, + onTokenExpiration: () => setIsLoggedIn(false), + }); + coreRef.current = newCore; + return newCore; }, [config, cookieAdapter]); const [isLoggedIn, setIsLoggedIn] = useState(core.isLoggedIn);