Skip to content

Commit 6d5b0bf

Browse files
committed
Open Google auth popup immediately
1 parent 5f8b276 commit 6d5b0bf

1 file changed

Lines changed: 6 additions & 100 deletions

File tree

app/account/page.tsx

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import { useRouter } from "next/navigation";
66
import {
77
createUserWithEmailAndPassword,
88
deleteUser,
9-
getRedirectResult,
109
getAdditionalUserInfo,
1110
GoogleAuthProvider,
1211
sendPasswordResetEmail,
1312
signInWithEmailAndPassword,
1413
signInWithPopup,
15-
signInWithRedirect,
1614
signOut,
1715
updateProfile,
1816
} from "firebase/auth";
@@ -50,15 +48,6 @@ const fieldClassName =
5048
const primaryButtonClassName =
5149
"group inline-flex min-h-16 w-full items-center justify-between bg-white px-5 font-semibold uppercase text-black transition-colors duration-300 hover:bg-[#8a2ae3] hover:text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-[#8a2ae3] active:translate-y-px disabled:cursor-not-allowed disabled:opacity-40";
5250
const CMS_PROFILE_URL = "https://cms.lap.onl/admin/profile";
53-
const GOOGLE_REDIRECT_MODE_KEY = "lap_google_redirect_mode";
54-
55-
function shouldUseGoogleRedirect() {
56-
if (typeof navigator === "undefined") return false;
57-
return (
58-
/iPad|iPhone|iPod/i.test(navigator.userAgent) ||
59-
(navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1)
60-
);
61-
}
6251

6352
async function checkCurrentDevice() {
6453
const payload = await getDeviceRiskPayload();
@@ -191,83 +180,6 @@ export default function AccountPage() {
191180
setHandle(profile?.handle || "");
192181
}, [profile?.handle]);
193182

194-
useEffect(() => {
195-
let cancelled = false;
196-
197-
const completeGoogleRedirect = async () => {
198-
const pendingMode = window.sessionStorage.getItem(GOOGLE_REDIRECT_MODE_KEY);
199-
200-
try {
201-
const credential = await getRedirectResult(auth);
202-
if (!credential || cancelled) return;
203-
204-
window.sessionStorage.removeItem(GOOGLE_REDIRECT_MODE_KEY);
205-
setBusy(true);
206-
setError("");
207-
setMessage("");
208-
209-
const redirectMode = pendingMode === "register" ? "register" : "signin";
210-
const isNewFirebaseUser = getAdditionalUserInfo(credential)?.isNewUser === true;
211-
212-
if (redirectMode === "signin" && isNewFirebaseUser) {
213-
await deleteUser(credential.user);
214-
if (!cancelled) {
215-
setMode("register");
216-
setError("No account was found. Create an account with Google below.");
217-
}
218-
return;
219-
}
220-
221-
const syncedRisk = await syncCurrentDevice();
222-
if (syncedRisk.blocked) {
223-
if (isNewFirebaseUser) {
224-
await deleteUser(credential.user).catch(() => undefined);
225-
}
226-
await signOut(auth).catch(() => undefined);
227-
if (!cancelled) {
228-
setError(
229-
syncedRisk.reason ||
230-
"This browser installation has been blocked due to Community Guidelines violations.",
231-
);
232-
}
233-
return;
234-
}
235-
236-
const existingProfile = await getExistingPublicProfile(credential.user);
237-
if (existingProfile) {
238-
await syncPublicUser(credential.user);
239-
await refreshProfile();
240-
}
241-
242-
if (!cancelled) {
243-
if (redirectMode === "register") {
244-
setMessage(
245-
existingProfile?.handle
246-
? `Your account already exists as @${existingProfile.handle}.`
247-
: "Account created. Add your photo and handle to finish.",
248-
);
249-
} else {
250-
setMessage(
251-
existingProfile
252-
? "Signed in."
253-
: "Welcome back. Finish your photo and handle to continue.",
254-
);
255-
}
256-
}
257-
} catch (nextError) {
258-
window.sessionStorage.removeItem(GOOGLE_REDIRECT_MODE_KEY);
259-
if (!cancelled) setError(friendlyAuthError(nextError));
260-
} finally {
261-
if (!cancelled) setBusy(false);
262-
}
263-
};
264-
265-
void completeGoogleRedirect();
266-
return () => {
267-
cancelled = true;
268-
};
269-
}, [refreshProfile]);
270-
271183
useEffect(() => {
272184
if (!isStaff || profile?.handle) return;
273185
setMessage((current) =>
@@ -402,24 +314,18 @@ export default function AccountPage() {
402314
setError("Please agree to the Terms of Service and Privacy Policy before creating an account with Google.");
403315
return;
404316
}
317+
if (mode === "register" && isDeviceBlocked) {
318+
setError(deviceBlockReason || "This browser installation has been blocked due to Community Guidelines violations.");
319+
return;
320+
}
405321
googleSignInStartedRef.current = true;
406322
setBusy(true);
407323
setError("");
408324
setMessage("");
409325
try {
410-
const deviceRisk = await checkCurrentDevice();
411-
if (mode === "register" && (deviceRisk.blocked || isDeviceBlocked)) {
412-
setError(deviceRisk.reason || deviceBlockReason || "This browser installation has been blocked due to Community Guidelines violations.");
413-
return;
414-
}
415-
416326
const provider = new GoogleAuthProvider();
417-
if (shouldUseGoogleRedirect()) {
418-
window.sessionStorage.setItem(GOOGLE_REDIRECT_MODE_KEY, mode);
419-
await signInWithRedirect(auth, provider);
420-
return;
421-
}
422-
327+
// Keep this as the first asynchronous operation after the user gesture.
328+
// Safari can block a popup if a network request runs before it opens.
423329
const credential = await signInWithPopup(auth, provider);
424330
const isNewFirebaseUser = getAdditionalUserInfo(credential)?.isNewUser === true;
425331

0 commit comments

Comments
 (0)