81 lines
2.5 KiB
JavaScript
81 lines
2.5 KiB
JavaScript
/**
|
|
* Firebase Authentication Logic for Intaleq Dashboard
|
|
*/
|
|
|
|
const auth = {
|
|
firebaseAuth: null,
|
|
currentUser: null,
|
|
idToken: null,
|
|
|
|
init: () => {
|
|
console.log('🔐 Initializing Auth Module...');
|
|
|
|
// 1. Initialize Firebase
|
|
firebase.initializeApp(firebaseConfig);
|
|
auth.firebaseAuth = firebase.auth();
|
|
|
|
// 2. Listen for Auth Changes
|
|
auth.firebaseAuth.onAuthStateChanged(async (user) => {
|
|
if (user) {
|
|
console.log('✅ User logged in:', user.email);
|
|
auth.currentUser = user;
|
|
auth.idToken = await user.getIdToken();
|
|
|
|
// Show Dashboard, Hide Login
|
|
auth.toggleUI(true);
|
|
|
|
// Initialize main app data
|
|
app.onAuthenticated();
|
|
} else {
|
|
console.log('❌ No active session.');
|
|
auth.currentUser = null;
|
|
auth.idToken = null;
|
|
|
|
// Show Login, Hide Dashboard
|
|
auth.toggleUI(false);
|
|
}
|
|
});
|
|
},
|
|
|
|
signInWithGoogle: async () => {
|
|
const provider = new firebase.auth.GoogleAuthProvider();
|
|
try {
|
|
await auth.firebaseAuth.signInWithPopup(provider);
|
|
} catch (error) {
|
|
console.error('Sign-in error:', error);
|
|
alert('Failed to sign in. Please try again.');
|
|
}
|
|
},
|
|
|
|
signOut: async () => {
|
|
try {
|
|
await auth.firebaseAuth.signOut();
|
|
} catch (error) {
|
|
console.error('Sign-out error:', error);
|
|
}
|
|
},
|
|
|
|
toggleUI: (isAuthenticated) => {
|
|
const loginSection = document.getElementById('login-section');
|
|
const mainSidebar = document.getElementById('main-sidebar');
|
|
const mainContent = document.getElementById('main-content');
|
|
|
|
if (isAuthenticated) {
|
|
if (loginSection) loginSection.classList.add('hidden');
|
|
if (mainSidebar) mainSidebar.classList.remove('hidden');
|
|
if (mainContent) mainContent.classList.remove('hidden');
|
|
} else {
|
|
if (loginSection) loginSection.classList.remove('hidden');
|
|
if (mainSidebar) mainSidebar.classList.add('hidden');
|
|
if (mainContent) mainContent.classList.add('hidden');
|
|
}
|
|
},
|
|
|
|
getAuthHeader: () => {
|
|
return auth.idToken ? { 'Authorization': `Bearer ${auth.idToken}` } : {};
|
|
}
|
|
};
|
|
|
|
// Start Auth on Load
|
|
document.addEventListener('DOMContentLoaded', auth.init);
|