PLEASE JUST WORK ON THE APP MF
This commit is contained in:
parent
a2842fbf7c
commit
e9a15d812f
1 changed files with 14 additions and 13 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
import { auth, provider } from './config.js';
|
import { auth, provider } from './config.js';
|
||||||
import {
|
import {
|
||||||
signInWithPopup,
|
signInWithPopup,
|
||||||
|
signInWithRedirect,
|
||||||
|
getRedirectResult,
|
||||||
signOut as firebaseSignOut,
|
signOut as firebaseSignOut,
|
||||||
onAuthStateChanged,
|
onAuthStateChanged,
|
||||||
signInWithEmailAndPassword,
|
signInWithEmailAndPassword,
|
||||||
|
|
@ -13,23 +15,26 @@ export class AuthManager {
|
||||||
this.user = null;
|
this.user = null;
|
||||||
this.unsubscribe = null;
|
this.unsubscribe = null;
|
||||||
this.authListeners = [];
|
this.authListeners = [];
|
||||||
|
this.isTauri = window.__TAURI__ !== undefined;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if (!auth) return;
|
if (!auth) return;
|
||||||
|
|
||||||
|
if (this.isTauri) {
|
||||||
|
getRedirectResult(auth).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
this.unsubscribe = onAuthStateChanged(auth, (user) => {
|
this.unsubscribe = onAuthStateChanged(auth, (user) => {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.updateUI(user);
|
this.updateUI(user);
|
||||||
|
|
||||||
this.authListeners.forEach((listener) => listener(user));
|
this.authListeners.forEach((listener) => listener(user));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onAuthStateChanged(callback) {
|
onAuthStateChanged(callback) {
|
||||||
this.authListeners.push(callback);
|
this.authListeners.push(callback);
|
||||||
// If we already have a user state, trigger immediately
|
|
||||||
if (this.user !== null) {
|
if (this.user !== null) {
|
||||||
callback(this.user);
|
callback(this.user);
|
||||||
}
|
}
|
||||||
|
|
@ -40,10 +45,12 @@ export class AuthManager {
|
||||||
alert('Firebase is not configured. Please check console.');
|
alert('Firebase is not configured. Please check console.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (this.isTauri) {
|
||||||
|
await signInWithRedirect(auth, provider);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const result = await signInWithPopup(auth, provider);
|
const result = await signInWithPopup(auth, provider);
|
||||||
// The onAuthStateChanged listener will handle the rest
|
|
||||||
return result.user;
|
return result.user;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error);
|
console.error('Login failed:', error);
|
||||||
|
|
@ -84,10 +91,8 @@ export class AuthManager {
|
||||||
|
|
||||||
async signOut() {
|
async signOut() {
|
||||||
if (!auth) return;
|
if (!auth) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await firebaseSignOut(auth);
|
await firebaseSignOut(auth);
|
||||||
// The onAuthStateChanged listener will handle the rest
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Logout failed:', error);
|
console.error('Logout failed:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|
@ -101,29 +106,25 @@ export class AuthManager {
|
||||||
const emailContainer = document.getElementById('email-auth-container');
|
const emailContainer = document.getElementById('email-auth-container');
|
||||||
const emailToggleBtn = document.getElementById('toggle-email-auth-btn');
|
const emailToggleBtn = document.getElementById('toggle-email-auth-btn');
|
||||||
|
|
||||||
if (!connectBtn) return; // UI might not be rendered yet
|
if (!connectBtn) return;
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
connectBtn.textContent = 'Sign Out';
|
connectBtn.textContent = 'Sign Out';
|
||||||
connectBtn.classList.add('danger');
|
connectBtn.classList.add('danger');
|
||||||
connectBtn.onclick = () => this.signOut();
|
connectBtn.onclick = () => this.signOut();
|
||||||
|
|
||||||
if (clearDataBtn) clearDataBtn.style.display = 'block';
|
if (clearDataBtn) clearDataBtn.style.display = 'block';
|
||||||
if (emailContainer) emailContainer.style.display = 'none';
|
if (emailContainer) emailContainer.style.display = 'none';
|
||||||
if (emailToggleBtn) emailToggleBtn.style.display = 'none';
|
if (emailToggleBtn) emailToggleBtn.style.display = 'none';
|
||||||
|
|
||||||
if (statusText) statusText.textContent = `Signed in as ${user.email}`;
|
if (statusText) statusText.textContent = `Signed in as ${user.email}`;
|
||||||
} else {
|
} else {
|
||||||
connectBtn.textContent = 'Connect with Google';
|
connectBtn.textContent = 'Connect with Google';
|
||||||
connectBtn.classList.remove('danger');
|
connectBtn.classList.remove('danger');
|
||||||
connectBtn.onclick = () => this.signInWithGoogle();
|
connectBtn.onclick = () => this.signInWithGoogle();
|
||||||
|
|
||||||
if (clearDataBtn) clearDataBtn.style.display = 'none';
|
if (clearDataBtn) clearDataBtn.style.display = 'none';
|
||||||
if (emailToggleBtn) emailToggleBtn.style.display = 'inline-block';
|
if (emailToggleBtn) emailToggleBtn.style.display = 'inline-block';
|
||||||
|
|
||||||
if (statusText) statusText.textContent = 'Sync your library across devices';
|
if (statusText) statusText.textContent = 'Sync your library across devices';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const authManager = new AuthManager();
|
export const authManager = new AuthManager();
|
||||||
Loading…
Reference in a new issue