projet
This commit is contained in:
84
services/firebaseService.js
Normal file
84
services/firebaseService.js
Normal file
@@ -0,0 +1,84 @@
|
||||
// Firebase V9 ESM seulement
|
||||
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js";
|
||||
import {
|
||||
getAuth,
|
||||
createUserWithEmailAndPassword,
|
||||
signInWithEmailAndPassword,
|
||||
onAuthStateChanged,
|
||||
signOut
|
||||
} from "https://www.gstatic.com/firebasejs/9.6.1/firebase-auth.js";
|
||||
|
||||
import {
|
||||
getFirestore,
|
||||
collection,
|
||||
addDoc,
|
||||
doc,
|
||||
setDoc
|
||||
} from "https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js";
|
||||
|
||||
const firebaseConfig = {
|
||||
apiKey: "AIzaSyBXKB6AGbTN95lOLrIVpWrSIK_uL_C6GUA",
|
||||
authDomain: "music-app-riotjs.firebaseapp.com",
|
||||
projectId: "music-app-riotjs",
|
||||
storageBucket: "music-app-riotjs.firebasestorage.app",
|
||||
messagingSenderId: "483729603961",
|
||||
appId: "1:483729603961:web:122965ce7dbeb2e85103a8",
|
||||
measurementId: "G-18P6WCT80"
|
||||
};
|
||||
|
||||
const app = initializeApp(firebaseConfig);
|
||||
const auth = getAuth(app);
|
||||
const db = getFirestore(app);
|
||||
|
||||
export async function login(email, password) {
|
||||
await signInWithEmailAndPassword(auth, email, password);
|
||||
}
|
||||
|
||||
export async function sign(email, password) {
|
||||
const userCred = await createUserWithEmailAndPassword(auth, email, password);
|
||||
await adduserdata(userCred.user.email);
|
||||
}
|
||||
|
||||
export async function adduserdata(email_user) {
|
||||
await addDoc(collection(db, "users"), {
|
||||
email: email_user
|
||||
});
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
await signOut(auth);
|
||||
}
|
||||
|
||||
window.observeAuthState = function(callback) {
|
||||
onAuthStateChanged(auth, callback);
|
||||
};
|
||||
|
||||
export async function favorite(releaseId) {
|
||||
const user = auth.currentUser;
|
||||
if (!user) return;
|
||||
await addDoc(collection(db, "users", user.email, "favorites"), {
|
||||
id: releaseId
|
||||
});
|
||||
}
|
||||
|
||||
export async function getFavorites() {
|
||||
const user = auth.currentUser;
|
||||
if (!user) {
|
||||
console.warn("Aucun utilisateur connecté.");
|
||||
return [];
|
||||
}
|
||||
|
||||
const userDocRef = doc(db, "users", user.email);
|
||||
const favsCollection = collection(userDocRef, "favorites");
|
||||
|
||||
const snapshot = await getDocs(favsCollection);
|
||||
const favorites = snapshot.docs.map(doc => doc.data());
|
||||
|
||||
return favorites;
|
||||
}
|
||||
|
||||
window.login = login;
|
||||
window.sign = sign;
|
||||
window.logout = logout;
|
||||
window.favorite = favorite;
|
||||
window.getFavorites = getFavorites;
|
||||
Reference in New Issue
Block a user