Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 47 additions & 0 deletions packages/shared/src/components/auth/AuthOptionsInner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ function AuthOptionsInner({
const [isRegistration, setIsRegistration] = useState(false);
const [isSocialAuthLoading, setIsSocialAuthLoading] = useState(false);
const windowPopup = useRef<Window | null>(null);
const popupCheckIntervalRef = useRef<ReturnType<typeof setInterval> | null>(
null,
);
const authFlowCompletedRef = useRef(false);

const clearPopupCheck = () => {
if (popupCheckIntervalRef.current) {
clearInterval(popupCheckIntervalRef.current);
popupCheckIntervalRef.current = null;
}
};

useEffect(() => {
return () => {
clearPopupCheck();
};
}, []);

const checkForOnboardedUser = async (data: LoggedUser) => {
onAuthStateUpdate({ isLoading: true });
Expand Down Expand Up @@ -383,6 +400,21 @@ function AuthOptionsInner({
};

const handleLoginMessage = async (e?: MessageEvent) => {
authFlowCompletedRef.current = true;
clearPopupCheck();
const popup = windowPopup.current;
windowPopup.current = null;
if (popup && !popup.closed) {
popup.close();
// Retry after a short delay — some browsers defer the close when the
// popup is still settling after a redirect chain.
setTimeout(() => {
if (!popup.closed) {
popup.close();
}
}, 300);
}

const callbackError = getSocialAuthCallbackError(e?.data);
if (callbackError) {
setIsSocialAuthLoading(false);
Expand Down Expand Up @@ -556,6 +588,21 @@ function AuthOptionsInner({
windowPopup.current.location.href = socialUrl;
await setChosenProvider(provider);
onAuthStateUpdate?.({ isLoading: true });

authFlowCompletedRef.current = false;
clearPopupCheck();
const popup = windowPopup.current;
popupCheckIntervalRef.current = setInterval(() => {
if (!popup || popup.closed) {
clearPopupCheck();
if (!authFlowCompletedRef.current) {
setIsSocialAuthLoading(false);
onAuthStateUpdate?.({ isLoading: false });
displayToast(SOCIAL_AUTH_RETRY_MESSAGE);
}
windowPopup.current = null;
}
}, 500);
};

const onProviderMessage = async (e: MessageEvent) => {
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/lib/betterAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const getBetterAuthSocialRedirect = async (
{
provider,
callbackURL: absoluteCallbackURL,
errorCallbackURL: absoluteCallbackURL,
disableRedirect: true,
...(additionalData && { additionalData }),
},
Expand Down
5 changes: 4 additions & 1 deletion packages/webapp/pages/callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ function CallbackPage(): ReactElement | null {
// while still allowing a script-opened tab to close itself. Try the
// close first and only fall back to a redirect if we know there is no
// opener to return to.
if (!window.opener) {
// Skip the redirect when there are error params — the opener handles
// closing the popup and showing the error toast.
const hasError = urlSearchParams.has('error');
if (!window.opener && !hasError) {
setTimeout(() => {
window.location.replace('/onboarding');
}, 300);
Expand Down
Loading