Spaces:
Sleeping
Sleeping
Download static/js/auth.js from Presight-AI/app: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/Presight-AI/app/resolve/main/static/js/auth.js
- Command line
-
hf download hf://spaces/Presight-AI/app/static/js/auth.js
-
curl -L -o auth.js https://huggingface.co/spaces/Presight-AI/app/resolve/main/static/js/auth.js
2.92 kB
| import { auth, googleProvider, db } from './firebase-config.js'; | |
| import { | |
| createUserWithEmailAndPassword, | |
| signInWithEmailAndPassword, | |
| signInWithPopup, | |
| signOut, | |
| onAuthStateChanged | |
| } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-auth.js"; | |
| import { doc, setDoc, getDoc, serverTimestamp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-firestore.js"; | |
| // --- Authentication Functions --- | |
| export async function registerUser(email, password) { | |
| try { | |
| const userCredential = await createUserWithEmailAndPassword(auth, email, password); | |
| const user = userCredential.user; | |
| // Create user document in Firestore | |
| await createUserDocument(user); | |
| return user; | |
| } catch (error) { | |
| console.error("Registration Error:", error); | |
| throw error; | |
| } | |
| } | |
| export async function loginUser(email, password) { | |
| try { | |
| const userCredential = await signInWithEmailAndPassword(auth, email, password); | |
| return userCredential.user; | |
| } catch (error) { | |
| console.error("Login Error:", error); | |
| throw error; | |
| } | |
| } | |
| export async function googleLogin() { | |
| try { | |
| const result = await signInWithPopup(auth, googleProvider); | |
| const user = result.user; | |
| // Check if user document exists, if not create it | |
| const userDocRef = doc(db, "users", user.uid); | |
| const userDoc = await getDoc(userDocRef); | |
| if (!userDoc.exists()) { | |
| await createUserDocument(user); | |
| } | |
| return user; | |
| } catch (error) { | |
| console.error("Google Login Error:", error); | |
| throw error; | |
| } | |
| } | |
| export async function logoutUser() { | |
| try { | |
| await signOut(auth); | |
| window.location.href = '/'; | |
| } catch (error) { | |
| console.error("Logout Error:", error); | |
| throw error; | |
| } | |
| } | |
| // --- Firestore User Management --- | |
| async function createUserDocument(user) { | |
| try { | |
| await setDoc(doc(db, "users", user.uid), { | |
| uid: user.uid, | |
| email: user.email, | |
| createdAt: serverTimestamp(), | |
| role: "user", | |
| scanningHistory: [] | |
| }); | |
| } catch (error) { | |
| console.error("Error creating user document:", error); | |
| throw error; | |
| } | |
| } | |
| // --- Auth State Observer --- | |
| export function initAuthObserver(protectedRoute = false) { | |
| onAuthStateChanged(auth, (user) => { | |
| if (user) { | |
| console.log("User is signed in:", user.email); | |
| // Redirect to app if on landing page | |
| if (window.location.pathname === '/' || window.location.pathname === '/login' || window.location.pathname === '/signup') { | |
| window.location.href = '/app'; | |
| } | |
| } else { | |
| console.log("User is signed out"); | |
| if (protectedRoute) { | |
| window.location.href = '/'; | |
| } | |
| } | |
| }); | |
| } | |