PageSourceSearch

https://ittle.co/firebase.js?v=1.0.1

js ittle.co collected 2026-09-24 09:51:47 UTC 7,126 bytes, 197 lines download raw bytes

1/* ==============================
2   ✅ FIREBASE APPLE, GOOGLE & EMAIL LINK AUTH INTEGRATION
3   ============================== */
4
5console.log("🔍 Running firebase.js version with main-content fix and email link auth");
6
7if (!firebase.apps.length) {
8    console.error("❌ Firebase app not initialized by index.html. Expected inline initialization.");
9} else {
10    console.log("ℹ️ Firebase app already initialized by index.html, reusing instance");
11    firebase.app();
12}
13
14const auth = firebase.auth();
15const googleProvider = new firebase.auth.GoogleAuthProvider();
16const appleProvider = new firebase.auth.OAuthProvider('apple.com');
17
18auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL)
19    .then(() => console.log("✅ Auth Persistence set to LOCAL"))
20    .catch((error) => console.error("❌ Auth Persistence Error:", error));
21
22auth.onAuthStateChanged(user => {
23    if (user) {
24        user.getIdToken(true).then(token => {
25            window.ittleAuthToken = token;
26            window.ittleAuthEmail = user.email;
27            console.log('✅ Exposed Firebase token to window.ittleAuthToken for user:', user.email);
28            // Dispatch event for extension
29            window.dispatchEvent(new CustomEvent('ittleAuthTokenAvailable', {
30                detail: { token }
31            }));
32            console.log('✅ Dispatched ittleAuthTokenAvailable event');
33        }).catch(error => {
34            console.error('❌ Error exposing Firebase token:', error);
35            window.ittleAuthToken = null;
36            window.ittleAuthEmail = null;
37        });
38    } else {
39        window.ittleAuthToken = null;
40        window.ittleAuthEmail = null;
41        console.log('ℹ️ No user signed in, cleared window.ittleAuthToken');
42    }
43});
44
45let signInInProgress = false;
46
47function isMobileDesktopView() {
48    return /Mobi|Android/i.test(navigator.userAgent) && window.innerWidth > 900;
49}
50
51async function signInWithGoogle() {
52    if (signInInProgress) {
53        console.warn("⚠️ Sign-in already in progress. Please wait...");
54        return;
55    }
56    signInInProgress = true;
57    console.log("🔄 Attempting Google Sign-In...");
58
59    try {
60        let result;
61        if (isMobileDesktopView()) {
62            console.warn("📲 Mobile Desktop View detected! Using redirect login...");
63            await auth.signInWithRedirect(googleProvider);
64        } else {
65            result = await auth.signInWithPopup(googleProvider);
66            console.log("✅ Logged in as:", result.user.email);
67        }
68    } catch (error) {
69        console.error("❌ Google Sign-In Error:", error.code, error.message);
70        throw error;
71    } finally {
72        signInInProgress = false;
73    }
74}
75
76async function signInWithApple() {
77    if (signInInProgress) {
78        console.warn("⚠️ Sign-in already in progress. Please wait...");
79        return;
80    }
81    signInInProgress = true;
82    console.log("🔄 Attempting Apple Sign-In...");
83
84    try {
85        const result = await auth.signInWithPopup(appleProvider);
86        console.log("✅ Logged in as:", result.user.email);
87    } catch (error) {
88        console.error("❌ Apple Sign-In Error:", error.code, error.message);
89        throw error;
90    } finally {
91        signInInProgress = false;
92    }
93}
94
95async function sendSignInLink(email) {
96    if (signInInProgress) {
97        console.warn("⚠️ Sign-in already in progress. Please wait...");
98        return;
99    }
100    signInInProgress = true;
101    console.log("🔄 Attempting Email Link Sign-In for:", email);
102
103    const actionCodeSettings = {
104        url: window.location.origin + '/finishSignIn.html',
105        handleCodeInApp: true
106    };
107
108    try {
109        await auth.sendSignInLinkToEmail(email, actionCodeSettings);
110        window.localStorage.setItem('emailForSignIn', email);
111        console.log("✅ Sign-in link sent to:", email);
112        alert('A sign-in link has been sent to your email. Please check your inbox.');
113    } catch (error) {
114        console.error("❌ Email Link Sign-In Error:", error.code, error.message);
115        throw error;
116    } finally {
117        signInInProgress = false;
118    }
119}
120
121async function handleEmailLinkSignIn() {
122    if (auth.isSignInWithEmailLink(window.location.href)) {
123        const email = window.localStorage.getItem('emailForSignIn');
124        if (!email) {
125            console.warn("⚠️ No email found in localStorage for email link sign-in");
126            alert('Please provide the email used for sign-in.');
127            return;
128        }
129
130        try {
131            const result = await auth.signInWithEmailLink(email, window.location.href);
132            window.localStorage.removeItem('emailForSignIn');
133            console.log("✅ Logged in with email link as:", result.user.email);
134            window.location.href = '/';
135        } catch (error) {
136            console.error("❌ Email Link Sign-In Completion Error:", error.code, error.message);
137            alert(`Error signing in: ${error.message}`);
138        }
139    }
140}
141
142function logOut() {
143    auth.signOut()
144        .then(() => {
145            console.log("✅ User signed out.");
146            updateUI(null);
147        })
148        .catch((error) => console.error("❌ Logout Error:", error.message));
149}
150
151function updateUI(user) {
152    const authStatus = document.getElementById("authStatus");
153    const loginButton = document.getElementById("login-btn");
154    const appleLoginButton = document.getElementById("apple-login-btn");
155    const emailLoginButton = document.getElementById("email-login-btn");
156    const logoutButton = document.getElementById("logout-btn");
157    const mainContent = document.getElementById("main-content");
158    const urlItems = document.querySelector(".url-items");
159
160    if (!authStatus || !loginButton || !appleLoginButton || !emailLoginButton || !logoutButton || !mainContent || !urlItems) {
161        console.warn(`⚠️ UI elements missing for ${window.location.pathname.split('/').pop() || 'index.html'}`);
162        return;
163    }
164
165    if (user) {
166        authStatus.innerText = `Logged in as: ${user.email}`;
167        loginButton.style.display = "none";
168        appleLoginButton.style.display = "none";
169        emailLoginButton.style.display = "none";
170        logoutButton.style.display = "block";
171        mainContent.style.display = "block";
172    } else {
173        authStatus.innerText = "Not logged in";
174        loginButton.style.display = "block";
175        appleLoginButton.style.display = "block";
176        emailLoginButton.style.display = "block";
177        logoutButton.style.display = "none";
178        mainContent.style.display = "none";
179        urlItems.innerHTML = "";
180    }
181    console.log("✅ UI updated successfully");
182}
183
184document.addEventListener('DOMContentLoaded', () => {
185    console.log("✅ DOM fully loaded and parsed");
186    auth.onAuthStateChanged((user) => {
187        console.log("🔄 Auth state changed:", user ? user.email : "No user");
188        updateUI(user);
189    });
190    handleEmailLinkSignIn();
191});
192
193window.signInWithGoogle = signInWithGoogle;
194window.signInWithApple = signInWithApple;
195window.sendSignInLink = sendSignInLink;
196window.logOut = logOut;
197window.auth = auth;

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.