Skip to content

Commit 092016c

Browse files
committed
fix: fallback to redirect in case of oauth popup failing
1 parent e6865ed commit 092016c

4 files changed

Lines changed: 21 additions & 39 deletions

File tree

packages/shared/src/components/auth/AuthOptionsInner.tsx

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@ import {
2121
betterAuthSendVerificationOTP,
2222
betterAuthVerifyEmailOTP,
2323
} from '../../lib/betterAuth';
24-
import {
25-
webappUrl,
26-
broadcastChannel,
27-
isTesting,
28-
isBrave,
29-
} from '../../lib/constants';
24+
import { webappUrl, broadcastChannel, isTesting } from '../../lib/constants';
3025
import { getUserDefaultTimezone } from '../../lib/timezones';
31-
import { isIOSNative, isMobile } from '../../lib/func';
26+
import { shouldUseSocialAuthPopup } from '../../lib/func';
3227
import { generateNameFromEmail } from '../../lib/strings';
3328
import { generateUsername, claimClaimableItem } from '../../graphql/users';
3429
import useRegistration from '../../hooks/useRegistration';
@@ -558,9 +553,9 @@ function AuthOptionsInner({
558553
await handleLoginMessage();
559554
return;
560555
}
561-
const isIOSApp = isIOSNative();
556+
const shouldUsePopup = shouldUseSocialAuthPopup();
562557
onAuthStateUpdate?.({ isLoading: true });
563-
if (!isIOSApp) {
558+
if (shouldUsePopup) {
564559
windowPopup.current = window.open();
565560
}
566561
const callbackURL = `${webappUrl}callback?login=true`;
@@ -585,21 +580,8 @@ function AuthOptionsInner({
585580
onAuthStateUpdate?.({ isLoading: false });
586581
return;
587582
}
588-
if (isIOSApp || (isBrave() && isMobile())) {
589-
window.location.href = socialUrl;
590-
return;
591-
}
592583
if (!windowPopup.current) {
593-
logEvent({
594-
event_name: authErrorEventName,
595-
extra: JSON.stringify({
596-
error: 'Failed to open social login window',
597-
origin: 'betterauth social popup',
598-
}),
599-
});
600-
setIsSocialAuthLoading(false);
601-
displayToast(SOCIAL_AUTH_RETRY_MESSAGE);
602-
onAuthStateUpdate?.({ isLoading: false });
584+
window.location.href = socialUrl;
603585
return;
604586
}
605587
windowPopup.current.location.href = socialUrl;

packages/shared/src/hooks/useLogin.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { labels } from '../lib';
2626
import { Origin } from '../lib/log';
2727
import { useEventListener } from './useEventListener';
2828
import { broadcastChannel, webappUrl } from '../lib/constants';
29-
import { isIOSNative } from '../lib/func';
29+
import { shouldUseSocialAuthPopup } from '../lib/func';
3030

3131
interface UseLogin {
3232
isPasswordLoginLoading?: boolean;
@@ -178,8 +178,8 @@ const useLogin = ({
178178
}
179179
return;
180180
}
181-
const isIOSApp = isIOSNative();
182-
const socialPopup = isIOSApp ? null : window.open();
181+
const shouldUsePopup = shouldUseSocialAuthPopup();
182+
const socialPopup = shouldUsePopup ? window.open() : null;
183183
const callbackURL = `${webappUrl}callback?login=true`;
184184
const { url: socialUrl, error } = await getBetterAuthSocialRedirectData(
185185
provider,
@@ -197,22 +197,11 @@ const useLogin = ({
197197
displayToast(labels.auth.error.generic);
198198
return;
199199
}
200-
if (isIOSApp) {
201-
window.location.href = socialUrl;
202-
return;
203-
}
204200
if (socialPopup) {
205201
socialPopup.location.href = socialUrl;
206202
return;
207203
}
208-
logEvent({
209-
event_name: AuthEventNames.LoginError,
210-
extra: JSON.stringify({
211-
error: 'Failed to open social login window',
212-
origin: Origin.BetterAuthSocialPopup,
213-
}),
214-
});
215-
displayToast(labels.auth.error.generic);
204+
window.location.href = socialUrl;
216205
},
217206
[displayToast, logEvent, refetchBoot, onUpdateSignBack],
218207
);

packages/shared/src/lib/func.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ export const isMobile = (): boolean =>
216216
export const shouldUseNativeShare = (): boolean =>
217217
'share' in globalThis?.navigator && isMobile();
218218

219+
export const shouldUseSocialAuthPopup = (): boolean => {
220+
if (isIOSNative()) {
221+
return false;
222+
}
223+
224+
if (isBrave() && isMobile()) {
225+
return false;
226+
}
227+
228+
return true;
229+
};
230+
219231
interface BroadcastMessage {
220232
eventKey: string;
221233
[key: string]: unknown;

packages/shared/src/lib/log.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ export enum Origin {
9191
BetterAuthNativeIdToken = 'betterauth native id token',
9292
BetterAuthNativeIdTokenBoot = 'betterauth native id token boot',
9393
BetterAuthSocialUrl = 'betterauth social url',
94-
BetterAuthSocialPopup = 'betterauth social popup',
9594
LoginTurnstile = 'login turnstile',
9695
}
9796

0 commit comments

Comments
 (0)