diff --git a/parcoursup/app.riot b/parcoursup/app.riot index 51b6009..7aa3b94 100644 --- a/parcoursup/app.riot +++ b/parcoursup/app.riot @@ -2,12 +2,15 @@ @@ -158,7 +161,8 @@ filters: {}, page: 1, limit: 20, - total: 0 + total: 0, + user: null }, onMounted() { @@ -174,6 +178,23 @@ var self = this + // Écouter les changements d'authentification Firebase + window.firebaseServices.onUserChanged(function(user) { + self.update({ user: user }) + + if (user) { + // Charger la sélection depuis Firestore + window.firebaseServices.loadUserData(user.uid).then(function(data) { + if (data && data.selection) { + self.update({ selectedFormations: data.selection }) + localStorage.setItem('selectionFormations', JSON.stringify(data.selection)) + } + }).catch(function(err) { + console.error('Erreur chargement Firestore:', err) + }) + } + }) + window.addEventListener('hashchange', function() { self.handleRoute() }) @@ -181,6 +202,26 @@ this.handleRoute() }, + onUserAuth() { + // Appelé après connexion/inscription réussie — l'écouteur onUserChanged gère le reste + }, + + onUserLogout() { + // On garde la sélection locale après déconnexion + }, + + async saveSelection(selection) { + localStorage.setItem('selectionFormations', JSON.stringify(selection)) + + if (this.state.user) { + try { + await window.firebaseServices.saveUserData(this.state.user.uid, { selection: selection }) + } catch (err) { + console.error('Erreur sauvegarde Firestore:', err) + } + } + }, + handleRoute() { var hash = window.location.hash || '#/' var path = hash.slice(1) || '/' @@ -330,7 +371,7 @@ if (!exists) { selection.push(formation) this.update({ selectedFormations: selection }) - localStorage.setItem('selectionFormations', JSON.stringify(selection)) + this.saveSelection(selection) } }, @@ -346,12 +387,12 @@ } this.update({ selectedFormations: newSelection }) - localStorage.setItem('selectionFormations', JSON.stringify(newSelection)) + this.saveSelection(newSelection) }, clearSelection() { this.update({ selectedFormations: [] }) - localStorage.setItem('selectionFormations', JSON.stringify([])) + this.saveSelection([]) }, updateNote(e) { this.update({ note: Number(e.target.value) }) }, diff --git a/parcoursup/components/auth-panel.riot b/parcoursup/components/auth-panel.riot new file mode 100644 index 0000000..d601e3f --- /dev/null +++ b/parcoursup/components/auth-panel.riot @@ -0,0 +1,141 @@ + + +
+ +
+ + +
+ { props.user.email } + +
+ + +
+
+ + +

{ state.mode === 'login' ? 'Connexion' : 'Créer un compte' }

+ +
+ + +
+ +
+
+ + +
+
+ + +
+ +
+ { state.error } +
+ + +
+
+
+ + +
diff --git a/parcoursup/firebase.js b/parcoursup/firebase.js index e69de29..e8c1330 100644 --- a/parcoursup/firebase.js +++ b/parcoursup/firebase.js @@ -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 +}; \ No newline at end of file diff --git a/parcoursup/index.html b/parcoursup/index.html index 134b207..dd62f89 100644 --- a/parcoursup/index.html +++ b/parcoursup/index.html @@ -19,6 +19,7 @@ +