80 lines
1.8 KiB
JavaScript
80 lines
1.8 KiB
JavaScript
|
|
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js";
|
||
|
|
|
||
|
|
import {
|
||
|
|
getAuth,
|
||
|
|
createUserWithEmailAndPassword,
|
||
|
|
signInWithEmailAndPassword,
|
||
|
|
signOut,
|
||
|
|
onAuthStateChanged
|
||
|
|
} from "https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js";
|
||
|
|
|
||
|
|
import {
|
||
|
|
getFirestore,
|
||
|
|
doc,
|
||
|
|
setDoc,
|
||
|
|
getDoc
|
||
|
|
} from "https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js";
|
||
|
|
|
||
|
|
|
||
|
|
const firebaseConfig = {
|
||
|
|
apiKey: "AIzaSyDr1jMgGm0Oj_bOiWY-8Gy27IlzkmAzlOM",
|
||
|
|
authDomain: "parcoursupp-expl.firebaseapp.com",
|
||
|
|
projectId: "parcoursupp-expl",
|
||
|
|
storageBucket: "parcoursupp-expl.firebasestorage.app",
|
||
|
|
messagingSenderId: "973054617217",
|
||
|
|
appId: "1:973054617217:web:4d52af4280396976228f80"
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
const app = initializeApp(firebaseConfig);
|
||
|
|
const auth = getAuth(app);
|
||
|
|
const db = getFirestore(app);
|
||
|
|
|
||
|
|
|
||
|
|
// Créer un compte avec email et mot de passe
|
||
|
|
async function createAccount(email, password) {
|
||
|
|
return createUserWithEmailAndPassword(auth, email, password);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Se connecter avec email et mot de passe
|
||
|
|
async function login(email, password) {
|
||
|
|
return signInWithEmailAndPassword(auth, email, password);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Se déconnecter
|
||
|
|
async function logout() {
|
||
|
|
return signOut(auth);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Écouter les changements d'état de connexion
|
||
|
|
function onUserChanged(callback) {
|
||
|
|
return onAuthStateChanged(auth, callback);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Sauvegarder les données d'un utilisateur dans Firestore
|
||
|
|
async function saveUserData(uid, data) {
|
||
|
|
await setDoc(doc(db, "users", uid), data, { merge: true });
|
||
|
|
}
|
||
|
|
|
||
|
|
// Charger les données d'un utilisateur depuis Firestore
|
||
|
|
async function loadUserData(uid) {
|
||
|
|
var snap = await getDoc(doc(db, "users", uid));
|
||
|
|
|
||
|
|
if (snap.exists()) {
|
||
|
|
return snap.data();
|
||
|
|
} else {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
export {
|
||
|
|
auth,
|
||
|
|
db,
|
||
|
|
createAccount,
|
||
|
|
login,
|
||
|
|
logout,
|
||
|
|
onUserChanged,
|
||
|
|
saveUserData,
|
||
|
|
loadUserData
|
||
|
|
};
|