@@ -76,7 +76,7 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
7676 const [ state , dispatch ] = useState < AuthStateInterface > ( AuthClient . getState ( ) ) ;
7777 const [ initialized , setInitialized ] = useState ( false ) ;
7878
79- const config = useMemo (
79+ const mergedConfig = useMemo (
8080 ( ) : AuthReactConfig => ( { ...defaultConfig , ...passedConfig } ) , [ passedConfig ]
8181 ) ;
8282
@@ -90,6 +90,16 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
9090 params : Record < string , unknown >
9191 }
9292 ) : Promise < BasicUserInfo > => {
93+ const _config = await AuthClient . getConfigData ( ) ;
94+
95+ // NOTE: With React 19 strict mode, the initialization logic runs twice, and there's an intermittent
96+ // issue where the config object is not getting stored in the storage layer with Vite scaffolding.
97+ // Hence, we need to check if the client is initialized but the config object is empty, and reinitialize.
98+ // Tracker: https://github.com/asgardeo/asgardeo-auth-react-sdk/issues/240
99+ if ( ! _config || Object . keys ( _config ) . length === 0 ) {
100+ await AuthClient . init ( mergedConfig ) ;
101+ }
102+
93103 try {
94104 setError ( null ) ;
95105 return await AuthClient . signIn (
@@ -147,12 +157,12 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
147157 if ( state . isAuthenticated ) {
148158 return ;
149159 }
160+
150161 ( async ( ) => {
151- setInitialized ( await AuthClient . init ( config ) ) ;
162+ setInitialized ( await AuthClient . init ( mergedConfig ) ) ;
152163 checkIsAuthenticated ( ) ;
153164 } ) ( ) ;
154-
155- } , [ config ] ) ;
165+ } , [ mergedConfig ] ) ;
156166
157167 /**
158168 * Try signing in when the component is mounted.
@@ -189,19 +199,21 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
189199
190200 // If `skipRedirectCallback` is not true, check if the URL has `code` and `session_state` params.
191201 // If so, initiate the sign in.
192- if ( ! config . skipRedirectCallback ) {
202+ if ( ! mergedConfig . skipRedirectCallback ) {
193203 let authParams : AuthParams = null ;
194204 if ( getAuthParams && typeof getAuthParams === "function" ) {
195205 authParams = await getAuthParams ( ) ;
196206 }
197207
198208 const url = new URL ( location . href ) ;
199209
200- if ( ( SPAUtils . hasAuthSearchParamsInURL ( )
201- && new URL ( url . origin + url . pathname ) . toString ( ) === new URL ( config ?. signInRedirectURL ) . toString ( ) )
202- || authParams ?. authorizationCode
203- || url . searchParams . get ( "error" ) )
204- {
210+ if (
211+ ( SPAUtils . hasAuthSearchParamsInURL ( ) &&
212+ new URL ( url . origin + url . pathname ) . toString ( ) ===
213+ new URL ( mergedConfig ?. signInRedirectURL ) . toString ( ) ) ||
214+ authParams ?. authorizationCode ||
215+ url . searchParams . get ( "error" )
216+ ) {
205217 try {
206218 await signIn (
207219 { callOnlyOnRedirect : true } ,
@@ -222,11 +234,11 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
222234 return ;
223235 }
224236
225- if ( ! config . disableAutoSignIn && await AuthClient . isSessionActive ( ) ) {
237+ if ( ! mergedConfig . disableAutoSignIn && await AuthClient . isSessionActive ( ) ) {
226238 signIn ( ) ;
227239 }
228240
229- if ( config . disableTrySignInSilently || isSignedOut ) {
241+ if ( mergedConfig . disableTrySignInSilently || isSignedOut ) {
230242 dispatch ( { ...state , isLoading : false } ) ;
231243
232244 return ;
@@ -249,7 +261,7 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
249261 } ) ;
250262 } ) ( ) ;
251263
252- } , [ config ] ) ;
264+ } , [ mergedConfig ] ) ;
253265
254266 /**
255267 * Check if the user is authenticated and update the state.isAuthenticated value.
@@ -291,6 +303,7 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
291303 value = { {
292304 disableHttpHandler,
293305 enableHttpHandler,
306+ error,
294307 getAccessToken,
295308 getBasicUserInfo,
296309 getDecodedIDPIDToken,
@@ -309,8 +322,7 @@ const AuthProvider: FunctionComponent<PropsWithChildren<AuthProviderPropsInterfa
309322 signOut,
310323 state,
311324 trySignInSilently,
312- updateConfig,
313- error
325+ updateConfig
314326 } }
315327 >
316328 { initialized ? children : fallback ?? null }
0 commit comments