File size: 2,920 Bytes
e2a1a46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 = '/';
            }
        }
    });
}