Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix react-sdk issue where isLoggedIn being set to false after successful token refresh #169

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions packages/sdk-react/src/components/providers/FusionAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -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 {
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