130 lines
No EOL
4.1 KiB
JavaScript
130 lines
No EOL
4.1 KiB
JavaScript
// js/firebase/auth.js
|
|
import { auth, provider } from './config.js';
|
|
import {
|
|
signInWithPopup,
|
|
signInWithRedirect,
|
|
getRedirectResult,
|
|
signOut as firebaseSignOut,
|
|
onAuthStateChanged,
|
|
signInWithEmailAndPassword,
|
|
createUserWithEmailAndPassword,
|
|
} from 'https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js';
|
|
|
|
export class AuthManager {
|
|
constructor() {
|
|
this.user = null;
|
|
this.unsubscribe = null;
|
|
this.authListeners = [];
|
|
this.isTauri = window.__TAURI__ !== undefined;
|
|
this.init();
|
|
}
|
|
|
|
init() {
|
|
if (!auth) return;
|
|
|
|
if (this.isTauri) {
|
|
getRedirectResult(auth).catch(() => {});
|
|
}
|
|
|
|
this.unsubscribe = onAuthStateChanged(auth, (user) => {
|
|
this.user = user;
|
|
this.updateUI(user);
|
|
this.authListeners.forEach((listener) => listener(user));
|
|
});
|
|
}
|
|
|
|
onAuthStateChanged(callback) {
|
|
this.authListeners.push(callback);
|
|
if (this.user !== null) {
|
|
callback(this.user);
|
|
}
|
|
}
|
|
|
|
async signInWithGoogle() {
|
|
if (!auth) {
|
|
alert('Firebase is not configured. Please check console.');
|
|
return;
|
|
}
|
|
try {
|
|
if (this.isTauri) {
|
|
await signInWithRedirect(auth, provider);
|
|
return;
|
|
}
|
|
const result = await signInWithPopup(auth, provider);
|
|
return result.user;
|
|
} catch (error) {
|
|
console.error('Login failed:', error);
|
|
alert(`Login failed: ${error.message}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async signInWithEmail(email, password) {
|
|
if (!auth) {
|
|
alert('Firebase is not configured.');
|
|
return;
|
|
}
|
|
try {
|
|
const result = await signInWithEmailAndPassword(auth, email, password);
|
|
return result.user;
|
|
} catch (error) {
|
|
console.error('Email Login failed:', error);
|
|
alert(`Login failed: ${error.message}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async signUpWithEmail(email, password) {
|
|
if (!auth) {
|
|
alert('Firebase is not configured.');
|
|
return;
|
|
}
|
|
try {
|
|
const result = await createUserWithEmailAndPassword(auth, email, password);
|
|
return result.user;
|
|
} catch (error) {
|
|
console.error('Sign Up failed:', error);
|
|
alert(`Sign Up failed: ${error.message}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async signOut() {
|
|
if (!auth) return;
|
|
try {
|
|
await firebaseSignOut(auth);
|
|
} catch (error) {
|
|
console.error('Logout failed:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
updateUI(user) {
|
|
const connectBtn = document.getElementById('firebase-connect-btn');
|
|
const clearDataBtn = document.getElementById('firebase-clear-cloud-btn');
|
|
const statusText = document.getElementById('firebase-status');
|
|
const emailContainer = document.getElementById('email-auth-container');
|
|
const emailToggleBtn = document.getElementById('toggle-email-auth-btn');
|
|
|
|
if (!connectBtn) return;
|
|
|
|
if (user) {
|
|
connectBtn.textContent = 'Sign Out';
|
|
connectBtn.classList.add('danger');
|
|
connectBtn.onclick = () => this.signOut();
|
|
if (clearDataBtn) clearDataBtn.style.display = 'block';
|
|
if (emailContainer) emailContainer.style.display = 'none';
|
|
if (emailToggleBtn) emailToggleBtn.style.display = 'none';
|
|
if (statusText) statusText.textContent = `Signed in as ${user.email}`;
|
|
} else {
|
|
connectBtn.textContent = 'Connect with Google';
|
|
connectBtn.classList.remove('danger');
|
|
connectBtn.onclick = () => this.signInWithGoogle();
|
|
if (clearDataBtn) clearDataBtn.style.display = 'none';
|
|
if (emailToggleBtn) emailToggleBtn.style.display = 'inline-block';
|
|
if (statusText) statusText.textContent = 'Sync your library across devices';
|
|
}
|
|
}
|
|
}
|
|
|
|
export const authManager = new AuthManager(); |