ajout de la persistance des données et authentification
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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);
|
||||
|
||||
async function createAccount(email, password) {
|
||||
return createUserWithEmailAndPassword(auth, email, password);
|
||||
}
|
||||
|
||||
async function login(email, password) {
|
||||
return signInWithEmailAndPassword(auth, email, password);
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
return signOut(auth);
|
||||
}
|
||||
|
||||
function onUserChanged(callback) {
|
||||
return onAuthStateChanged(auth, callback);
|
||||
}
|
||||
|
||||
async function saveUserData(uid, data) {
|
||||
await setDoc(doc(db, "users", uid), data, { merge: true });
|
||||
}
|
||||
|
||||
async function loadUserData(uid) {
|
||||
const snap = await getDoc(doc(db, "users", uid));
|
||||
return snap.exists() ? snap.data() : null;
|
||||
}
|
||||
|
||||
export {
|
||||
auth,
|
||||
db,
|
||||
createAccount,
|
||||
login,
|
||||
logout,
|
||||
onUserChanged,
|
||||
saveUserData,
|
||||
loadUserData
|
||||
};
|
||||
Reference in New Issue
Block a user