Skip to content

Commit

Permalink
Fix react-sdk isLoggedIn being set to false after token refresh issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLo123 committed Sep 19, 2024
1 parent ece7b9f commit f0e1698
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions packages/sdk-react/src/components/providers/FusionAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -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';

Check warning on line 9 in packages/sdk-react/src/components/providers/FusionAuthProvider.tsx

View workflow job for this annotation

GitHub Actions / check (20.x)

'CookieAdapter' is defined but never used

Expand All @@ -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<T = DefaultUserInfo>({
children,
...config
}: PropsWithChildren & FusionAuthProviderConfig) {
const cookieAdapter = useCookieAdapter(config);

const coreRef = useRef<SDKCore>();
const core: SDKCore = useMemo<SDKCore>(() => {
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);
Expand Down

0 comments on commit f0e1698

Please sign in to comment.