From aa4827b3ba96503e3445044f6b044d548888f7dc Mon Sep 17 00:00:00 2001 From: Jake Loew Date: Thu, 19 Sep 2024 13:38:50 -0600 Subject: [PATCH 1/2] Fix react-sdk `isLoggedIn` being set to false after token refresh issue --- .../providers/FusionAuthProvider.tsx | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx b/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx index 03016ae..f5e2f51 100644 --- a/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx +++ b/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx @@ -1,6 +1,12 @@ -import { PropsWithChildren, useContext, useMemo, useState } from 'react'; +import { + PropsWithChildren, + useContext, + useMemo, + useState, + useRef, +} from 'react'; -import { CookieAdapter, SDKCore } from '@fusionauth-sdk/core'; +import { SDKCore } from '@fusionauth-sdk/core'; import { FusionAuthProviderConfig } from './FusionAuthProviderConfig'; import { @@ -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); From 630bffee65f0a2348c03d726d9c38f50faefc38f Mon Sep 17 00:00:00 2001 From: Jake Loew Date: Fri, 20 Sep 2024 07:08:37 -0600 Subject: [PATCH 2/2] destructure config within sdk core instead of using rest operator --- .../providers/FusionAuthProvider.tsx | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx b/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx index f5e2f51..327a580 100644 --- a/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx +++ b/packages/sdk-react/src/components/providers/FusionAuthProvider.tsx @@ -6,7 +6,7 @@ import { useRef, } from 'react'; -import { SDKCore } from '@fusionauth-sdk/core'; +import { SDKConfig, SDKCore } from '@fusionauth-sdk/core'; import { FusionAuthProviderConfig } from './FusionAuthProviderConfig'; import { @@ -18,10 +18,49 @@ import { import { FusionAuthContext, UserInfo as DefaultUserInfo } from './Context'; import { FusionAuthProviderContext } from './FusionAuthProviderContext'; -function FusionAuthProvider({ - children, - ...config -}: PropsWithChildren & FusionAuthProviderConfig) { +function FusionAuthProvider( + props: PropsWithChildren & FusionAuthProviderConfig, +) { + const config: Omit = + useMemo( + () => ({ + serverUrl: props.serverUrl, + clientId: props.clientId, + redirectUri: props.redirectUri, + scope: props.scope, + postLogoutRedirectUri: props.postLogoutRedirectUri, + shouldAutoRefresh: props.shouldAutoRefresh, + shouldAutoFetchUserInfo: props.shouldAutoFetchUserInfo, + autoRefreshSecondsBeforeExpiry: props.autoRefreshSecondsBeforeExpiry, + onRedirect: props.onRedirect, + loginPath: props.loginPath, + logoutPath: props.logoutPath, + registerPath: props.registerPath, + tokenRefreshPath: props.tokenRefreshPath, + mePath: props.mePath, + accessTokenExpireCookieName: props.accessTokenExpireCookieName, + onAutoRefreshFailure: props.onAutoRefreshFailure, + }), + [ + props.serverUrl, + props.clientId, + props.redirectUri, + props.scope, + props.postLogoutRedirectUri, + props.shouldAutoRefresh, + props.shouldAutoFetchUserInfo, + props.autoRefreshSecondsBeforeExpiry, + props.onRedirect, + props.loginPath, + props.logoutPath, + props.registerPath, + props.tokenRefreshPath, + props.mePath, + props.accessTokenExpireCookieName, + props.onAutoRefreshFailure, + ], + ); + const cookieAdapter = useCookieAdapter(config); const coreRef = useRef(); @@ -70,7 +109,7 @@ function FusionAuthProvider({ return ( - {children} + {props.children} ); }