bookworm-bazaar / script.js
Earth2323's picture
add an option of users to log in using their gmails or create a normal account with us to be able to comment
a19f7db verified
Raw History Blame Contribute Delete
2.79 kB
// 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();
});