File size: 2,785 Bytes
a19f7db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Firebase configuration
const firebaseConfig = {
  apiKey: "AIzaSyDEXAMPLEEXAMPLEEXAMPLEEXAMPLE",
  authDomain: "bookworm-bazaar.firebaseapp.com",
  projectId: "bookworm-bazaar",
  storageBucket: "bookworm-bazaar.appspot.com",
  messagingSenderId: "123456789012",
  appId: "1:123456789012:web:EXAMPLEEXAMPLEEXAMPLE"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();

// Auth state observer
auth.onAuthStateChanged(user => {
  const authButtons = document.getElementById('auth-buttons');
  const userProfile = document.getElementById('user-profile');
  
  if (user) {
    // User is signed in
    authButtons.classList.add('hidden');
    userProfile.classList.remove('hidden');
    document.getElementById('user-avatar').src = user.photoURL || 'http://static.photos/people/200x200/6';
    document.getElementById('user-name').textContent = user.displayName || 'User';
    
    // Enable review form if on book detail page
    const reviewForm = document.getElementById('review-form');
    if (reviewForm) {
      reviewForm.classList.remove('hidden');
    }
  } else {
    // User is signed out
    authButtons.classList.remove('hidden');
    userProfile.classList.add('hidden');
    
    // Disable review form if on book detail page
    const reviewForm = document.getElementById('review-form');
    if (reviewForm) {
      reviewForm.classList.add('hidden');
    }
  }
});

// Google Sign-In
document.getElementById('login-btn')?.addEventListener('click', () => {
  const provider = new firebase.auth.GoogleAuthProvider();
  auth.signInWithPopup(provider)
    .catch(error => console.error('Google sign-in error:', error));
});

// Email/password sign up
document.getElementById('signup-btn')?.addEventListener('click', () => {
  Swal.fire({
    title: 'Create Account',
    html:
      '<input id="swal-name" class="swal2-input" placeholder="Full Name">' +
      '<input id="swal-email" class="swal2-input" placeholder="Email">' +
      '<input id="swal-password" class="swal2-input" placeholder="Password" type="password">',
    focusConfirm: false,
    preConfirm: () => {
      return auth.createUserWithEmailAndPassword(
        document.getElementById('swal-email').value,
        document.getElementById('swal-password').value
      ).then((userCredential) => {
        return userCredential.user.updateProfile({
          displayName: document.getElementById('swal-name').value
        });
      }).catch(error => {
        Swal.showValidationMessage(`Signup failed: ${error.message}`);
      });
    }
  });
});

// Logout
document.getElementById('logout-btn')?.addEventListener('click', () => {
  auth.signOut();
});

// Initialize Feather Icons
document.addEventListener('DOMContentLoaded', () => {
  feather.replace();
});