Skip to content

Commit ecf7c16

Browse files
authored
Merge pull request #283 from brionmario/fix-react-240
Retry the initialisation incase if the config data is not saved in the storage
2 parents c7c21ec + 834e42a commit ecf7c16

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"LICENSE"
4545
],
4646
"dependencies": {
47-
"@asgardeo/auth-spa": "^3.1.4"
47+
"@asgardeo/auth-spa": "^3.1.5"
4848
},
4949
"devDependencies": {
5050
"@babel/cli": "^7.19.3",

lib/src/api.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@ class AuthAPI {
6363
*
6464
* @param {Config} config - `dispatch` function from React Auth Context.
6565
*/
66-
public init(config: AuthClientConfig<Config>): Promise<boolean> {
67-
return this._client.initialize(config);
66+
public async init(config: AuthClientConfig<Config>): Promise<boolean> {
67+
return await this._client.initialize(config);
68+
}
69+
70+
/**
71+
* Method to get the configuration data.
72+
*
73+
* @returns {Promise<AuthClientConfig<Config>>} - A promise that resolves with the configuration data.
74+
*/
75+
public async getConfigData(): Promise<AuthClientConfig<Config>> {
76+
return await this._client.getConfigData();
6877
}
6978

7079
/**

lib/src/authenticate.tsx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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 }

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
resolved "https://registry.yarnpkg.com/@asgardeo/auth-js/-/auth-js-5.1.1.tgz#aa7454224a3dd45fc85add3eb4d32229f30232d1"
1616
integrity sha512-yIgBKvHbt9ENczm2n3FGPET/Xf0I9H+Jm6oMXb8ClB/5m5qygHLoF3bsiDlzNuw3wuz8RH8F+waRudVI+IoLtQ==
1717

18-
"@asgardeo/auth-spa@^3.1.4":
19-
version "3.1.4"
20-
resolved "https://registry.yarnpkg.com/@asgardeo/auth-spa/-/auth-spa-3.1.4.tgz#0fc5c7d3484be43c0273d1a6a2cedbdaa7036400"
21-
integrity sha512-ToDPbwNUY7hOK+vQbZWILe1n1gCxs6s+qZAUCjR0SmAEzf72rFHN6wv0Xg4e91MI8pb4xVJ3JmtJhDX4To8Tog==
18+
"@asgardeo/auth-spa@^3.1.5":
19+
version "3.1.5"
20+
resolved "https://registry.yarnpkg.com/@asgardeo/auth-spa/-/auth-spa-3.1.5.tgz#d6a2c9e46b8fec7e26bd543803f6afd28936a25c"
21+
integrity sha512-LoQA96oFQ7wegN9RoeYrhUsdxDPGcC8ys6drA5XstrupsM1tQ1698Dsfx4Pt22AEpiw4JwWoITZpIEVJzd4FvA==
2222
dependencies:
2323
"@asgardeo/auth-js" "^5.1.1"
2424
await-semaphore "^0.1.3"

0 commit comments

Comments
 (0)