-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGoogleProvider.tsx
More file actions
42 lines (37 loc) · 1.48 KB
/
Copy pathGoogleProvider.tsx
File metadata and controls
42 lines (37 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Google sign-in helper. Uses the centralized `auth` exported from firebase.config.
import { GoogleAuthProvider, signInWithPopup, signInWithRedirect } from "firebase/auth";
import { auth } from './firebase.config';
const googleProvider = new GoogleAuthProvider();
// Always prompt the account chooser so users can pick which Google account to sign in with.
// This prevents automatic selection of a previously-signed-in account.
googleProvider.setCustomParameters({ prompt: 'select_account' });
export const signInWithGoogle = async () => {
try {
await signInWithPopup(auth, googleProvider);
} catch (err) {
console.error('signInWithPopup failed:', err);
// Fallback to redirect when popup is blocked or unsupported in this environment.
const code = (err as any)?.code;
const popupErrorCodes = new Set([
'auth/popup-blocked',
'auth/popup-closed-by-user',
'auth/cancelled-popup-request',
'auth/operation-not-supported-in-this-environment'
]);
if (popupErrorCodes.has(code)) {
try {
await signInWithRedirect(auth, googleProvider);
} catch (redirectErr) {
console.error('signInWithRedirect failed as fallback:', redirectErr);
}
}
}
};
// Expose an explicit redirect sign-in for cases where popup flows are blocked
export const signInWithGoogleRedirect = async () => {
try {
await signInWithRedirect(auth, googleProvider);
} catch (err) {
console.error('signInWithGoogleRedirect failed:', err);
}
};