Files
MyIP/frontend/firebase-init.js
Claude 65ec652135 Refactor(auth): one auth domain per deployment
VITE_FIREBASE_AUTH_DOMAIN is each environment's own host, served by the
/__/auth proxy; the PWA-only VITE_FIREBASE_PWA_AUTH_DOMAIN and
utils/auth-domain.js go. Every OAuth provider lists the handler URL per
environment (Google and GitHub both take several redirect URIs).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013GbLTGMZ45MV78qd2tHoco
2026-10-06 01:38:29 +00:00

42 lines
1.7 KiB
JavaScript

// Firebase Auth bootstrap — env-gated AND lazy: the SDK chunk only loads on
// the first loadFirebaseAuth() call (signed-in boot, sign-in click, or the
// background auth probe), never on the visitor-critical path.
const env = import.meta.env ?? {};
const isFireBaseSet = !!env.VITE_FIREBASE_API_KEY && !!env.VITE_FIREBASE_AUTH_DOMAIN && !!env.VITE_FIREBASE_PROJECT_ID;
let authModulePromise = null;
// Resolves to the firebase/auth namespace plus the initialized `auth`
// instance (memoized), or null when the env isn't configured.
const loadFirebaseAuth = () => {
if (!isFireBaseSet) return Promise.resolve(null);
authModulePromise ??= Promise.all([
import('firebase/app'),
import('firebase/auth'),
]).then(([appModule, authModule]) => {
// Hostile client environments (extensions, filtered networks) can
// resolve a dynamic import to undefined instead of rejecting —
// convert that to a readable rejection so the retry path below runs.
if (!appModule?.initializeApp || !authModule?.getAuth) {
throw new Error('Firebase modules resolved without expected exports');
}
return {
...authModule,
auth: authModule.getAuth(appModule.initializeApp({
apiKey: env.VITE_FIREBASE_API_KEY,
authDomain: env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: env.VITE_FIREBASE_PROJECT_ID,
})),
};
}).catch((error) => {
// Don't memoize a transient chunk-load failure — the next auth
// action should retry the import instead of replaying the rejection.
authModulePromise = null;
throw error;
});
return authModulePromise;
};
export { isFireBaseSet, loadFirebaseAuth };