Skip to content

Commit 3f882c2

Browse files
authored
Merge pull request #174 from kinde-oss/fix/close-logout-in-popup
2 parents 3823fb9 + 27506ab commit 3f882c2

File tree

1 file changed

+65
-51
lines changed

1 file changed

+65
-51
lines changed

src/state/KindeProvider.tsx

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -552,66 +552,80 @@ export const KindeProvider = ({
552552

553553
const init = useCallback(async () => {
554554
if (initRef.current) return;
555-
await checkAuth({ domain, clientId });
556-
initRef.current = true;
557-
const params = new URLSearchParams(window.location.search);
558-
559-
if (params.has("error")) {
560-
const errorCode = params.get("error");
561-
if (errorCode?.toLowerCase() === "login_link_expired") {
562-
const reauthState = params.get("reauth_state");
563-
if (reauthState) {
564-
login({ reauthState: reauthState });
555+
try {
556+
try {
557+
await checkAuth({ domain, clientId });
558+
} catch (err) {
559+
console.warn("checkAuth failed:", err);
560+
setState((v: ProviderState) => ({ ...v, isLoading: false }));
561+
}
562+
initRef.current = true;
563+
const params = new URLSearchParams(window.location.search);
564+
565+
if (params.has("error")) {
566+
const errorCode = params.get("error");
567+
if (errorCode?.toLowerCase() === "login_link_expired") {
568+
const reauthState = params.get("reauth_state");
569+
if (reauthState) {
570+
login({ reauthState: reauthState });
571+
}
572+
return;
565573
}
574+
setState((val: ProviderState) => ({ ...val, isLoading: false }));
566575
return;
567576
}
568-
setState((val: ProviderState) => ({ ...val, isLoading: false }));
569-
return;
570-
}
571577

572-
if (
573-
(await storeState.localStorage.getSessionItem(
574-
storeState.LocalKeys.performingLogout,
575-
)) === "true"
576-
) {
577-
await storeState.localStorage.removeSessionItem(
578-
storeState.LocalKeys.performingLogout,
579-
);
580-
window.close();
581-
}
578+
if (
579+
(await storeState.localStorage.getSessionItem(
580+
storeState.LocalKeys.performingLogout,
581+
)) === "true"
582+
) {
583+
await storeState.localStorage.removeSessionItem(
584+
storeState.LocalKeys.performingLogout,
585+
);
586+
if (window.opener) {
587+
window.close();
588+
}
589+
}
582590

583-
const hasCode = params.has("code");
584-
const isOnRedirectUri = window.location.href.startsWith(redirectUri);
585-
if (!hasCode || !isOnRedirectUri) {
586-
try {
587-
const user = await getUserProfile();
588-
if (user) {
589-
setState((val: ProviderState) => ({
590-
...val,
591-
user,
592-
isAuthenticated: true,
593-
}));
591+
const hasCode = params.has("code");
592+
const isOnRedirectUri = window.location.href.startsWith(redirectUri);
593+
if (!hasCode || !isOnRedirectUri) {
594+
try {
595+
const user = await getUserProfile();
596+
if (user) {
597+
setState((val: ProviderState) => ({
598+
...val,
599+
user,
600+
isAuthenticated: true,
601+
}));
602+
}
603+
} catch (error) {
604+
console.warn("Error getting user profile", error);
605+
} finally {
606+
setState((val: ProviderState) => ({ ...val, isLoading: false }));
594607
}
595-
} catch (error) {
596-
console.warn("Error getting user profile", error);
597-
} finally {
598-
setState((val: ProviderState) => ({ ...val, isLoading: false }));
608+
return;
599609
}
600-
return;
601-
}
602610

603-
if (window.opener) {
604-
const searchParams = new URLSearchParams(window.location.search);
605-
window.opener.postMessage(
606-
{
607-
type: "KINDE_AUTH_RESULT",
608-
result: Object.fromEntries(searchParams.entries()),
609-
},
610-
window.location.origin,
611-
);
612-
window.close();
611+
if (window.opener) {
612+
const searchParams = new URLSearchParams(window.location.search);
613+
window.opener.postMessage(
614+
{
615+
type: "KINDE_AUTH_RESULT",
616+
result: Object.fromEntries(searchParams.entries()),
617+
},
618+
window.location.origin,
619+
);
620+
window.close();
621+
return;
622+
}
623+
await processAuthResult(new URLSearchParams(window.location.search));
624+
} finally {
625+
if (window.opener) {
626+
window.close();
627+
}
613628
}
614-
await processAuthResult(new URLSearchParams(window.location.search));
615629
}, [
616630
clientId,
617631
domain,

0 commit comments

Comments
 (0)