aditya-rAj19 commited on
Commit
55d2255
Β·
1 Parent(s): 763532f

fix: fall back to signInWithRedirect when popup is blocked

Browse files
frontend/src/firebase.js CHANGED
@@ -1,5 +1,5 @@
1
  import { initializeApp } from 'firebase/app';
2
- import { getAuth, GoogleAuthProvider, signInWithPopup, signOut, onAuthStateChanged } from 'firebase/auth';
3
 
4
  const firebaseConfig = {
5
  apiKey: "AIzaSyCF9t3xPf_Se4qkqAHFRoW-YqG8LfoQIpo",
@@ -15,4 +15,4 @@ const fbApp = initializeApp(firebaseConfig);
15
  const auth = getAuth(fbApp);
16
  const googleProvider = new GoogleAuthProvider();
17
 
18
- export { auth, googleProvider, signInWithPopup, signOut, onAuthStateChanged };
 
1
  import { initializeApp } from 'firebase/app';
2
+ import { getAuth, GoogleAuthProvider, signInWithPopup, signInWithRedirect, getRedirectResult, signOut, onAuthStateChanged } from 'firebase/auth';
3
 
4
  const firebaseConfig = {
5
  apiKey: "AIzaSyCF9t3xPf_Se4qkqAHFRoW-YqG8LfoQIpo",
 
15
  const auth = getAuth(fbApp);
16
  const googleProvider = new GoogleAuthProvider();
17
 
18
+ export { auth, googleProvider, signInWithPopup, signInWithRedirect, getRedirectResult, signOut, onAuthStateChanged };
frontend/src/pages/LandingPage.jsx CHANGED
@@ -1,7 +1,7 @@
1
  import { useState } from 'react';
2
  import { useNavigate } from 'react-router-dom';
3
  import { motion } from 'framer-motion';
4
- import { auth, googleProvider, signInWithPopup } from '../firebase';
5
  import useAuth, { saveSession, daysLeft } from '../hooks/useAuth';
6
  import Aurora from '../components/ui/Aurora';
7
  import SplitText from '../components/ui/SplitText';
@@ -49,14 +49,26 @@ export default function LandingPage() {
49
  const isSignedIn = !!user;
50
  const days = isSignedIn ? daysLeft() : 0;
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  const doGoogleSignIn = async () => {
53
  setSigningIn(true);
54
  setError('');
55
  try {
56
  const result = await signInWithPopup(auth, googleProvider);
57
- // Save session immediately with empty token so isSessionExpired() won't block
58
  saveSession(result.user, '');
59
- // Fetch real token in background β€” do NOT await before navigating
60
  result.user.getIdToken(false).then(t => localStorage.setItem('df_token', t)).catch(() => {});
61
  navigate('/app');
62
  } catch (err) {
@@ -64,9 +76,10 @@ export default function LandingPage() {
64
  if (err?.code === 'auth/popup-closed-by-user') {
65
  // user dismissed β€” no toast needed
66
  } else if (err?.code === 'auth/popup-blocked') {
67
- setError('Popup was blocked. Please allow popups for this site and try again.');
 
68
  } else if (err?.code === 'auth/unauthorized-domain') {
69
- setError('This domain is not authorised in Firebase. Add localhost to Authorised Domains in Firebase Console.');
70
  } else if (err?.code === 'auth/operation-not-allowed') {
71
  setError('Google sign-in is not enabled. Enable it in Firebase Console β†’ Authentication β†’ Sign-in method.');
72
  } else {
 
1
  import { useState } from 'react';
2
  import { useNavigate } from 'react-router-dom';
3
  import { motion } from 'framer-motion';
4
+ import { auth, googleProvider, signInWithPopup, signInWithRedirect, getRedirectResult } from '../firebase';
5
  import useAuth, { saveSession, daysLeft } from '../hooks/useAuth';
6
  import Aurora from '../components/ui/Aurora';
7
  import SplitText from '../components/ui/SplitText';
 
49
  const isSignedIn = !!user;
50
  const days = isSignedIn ? daysLeft() : 0;
51
 
52
+ // Handle redirect result when returning from Google sign-in redirect
53
+ useEffect(() => {
54
+ getRedirectResult(auth).then(result => {
55
+ if (!result) return;
56
+ saveSession(result.user, '');
57
+ result.user.getIdToken(false).then(t => localStorage.setItem('df_token', t)).catch(() => {});
58
+ navigate('/app');
59
+ }).catch(err => {
60
+ if (err?.code && err.code !== 'auth/popup-closed-by-user') {
61
+ setError(`Sign in failed: ${err.code}`);
62
+ }
63
+ });
64
+ }, [navigate]);
65
+
66
  const doGoogleSignIn = async () => {
67
  setSigningIn(true);
68
  setError('');
69
  try {
70
  const result = await signInWithPopup(auth, googleProvider);
 
71
  saveSession(result.user, '');
 
72
  result.user.getIdToken(false).then(t => localStorage.setItem('df_token', t)).catch(() => {});
73
  navigate('/app');
74
  } catch (err) {
 
76
  if (err?.code === 'auth/popup-closed-by-user') {
77
  // user dismissed β€” no toast needed
78
  } else if (err?.code === 'auth/popup-blocked') {
79
+ // Popup blocked β€” fall back to redirect (no popup permission needed)
80
+ await signInWithRedirect(auth, googleProvider);
81
  } else if (err?.code === 'auth/unauthorized-domain') {
82
+ setError('This domain is not authorised in Firebase. Add it to Authorised Domains in Firebase Console.');
83
  } else if (err?.code === 'auth/operation-not-allowed') {
84
  setError('Google sign-in is not enabled. Enable it in Firebase Console β†’ Authentication β†’ Sign-in method.');
85
  } else {