|
| 1 | +import { initializeApp } from "firebase/app"; |
| 2 | +import { getAuth, GoogleAuthProvider, onAuthStateChanged } from "firebase/auth"; |
| 3 | +import { get, getDatabase, ref, set, serverTimestamp } from "firebase/database"; |
| 4 | + |
| 5 | +// Your web app's Firebase configuration |
| 6 | +const firebaseConfig = { |
| 7 | + apiKey: "AIzaSyCBckVI9nhAP62u5jZJW3F4SLulUv7znis", |
| 8 | + authDomain: "findmynextcourse.firebaseapp.com", |
| 9 | + databaseURL: "https://findmynextcourse-default-rtdb.europe-west1.firebasedatabase.app", |
| 10 | + projectId: "findmynextcourse", |
| 11 | + storageBucket: "findmynextcourse.firebasestorage.app", |
| 12 | + messagingSenderId: "893484115963", |
| 13 | + appId: "1:893484115963:web:59ac087d280dec919ccd5e" |
| 14 | + }; |
| 15 | + |
| 16 | + |
| 17 | +// Initialize Firebase |
| 18 | +const app = initializeApp(firebaseConfig); |
| 19 | +export const auth = getAuth(app); |
| 20 | +export const db = getDatabase(app); |
| 21 | +// export const googleProvider = new GoogleAuthProvider(); |
| 22 | +// googleProvider.addScope("profile"); |
| 23 | +// googleProvider.addScope("email"); |
| 24 | + |
| 25 | +// fetches all relevant information to create the model |
| 26 | +async function firebaseToModel(model) { |
| 27 | + const courses = await fetchCourses(); |
| 28 | + model.setCourses(courses); |
| 29 | +} |
| 30 | + |
| 31 | +export function connectToFirebase(model) { |
| 32 | + // onAuthStateChanged(auth, (user) => { |
| 33 | + // model.setUser(user); |
| 34 | + // }); !can be used for auth! |
| 35 | + |
| 36 | + firebaseToModel(model); |
| 37 | +} |
| 38 | + |
| 39 | +export async function addCourse(course){ |
| 40 | + if(!course?.courseCode) |
| 41 | + return; |
| 42 | + const myRef = ref(db, `courses/${course.courseCode}`); |
| 43 | + await set(myRef, course); |
| 44 | +} |
| 45 | + |
| 46 | +export async function fetchCourses() { |
| 47 | + const myRef = ref(db, `courses`); |
| 48 | + const snapshot = await get(myRef); |
| 49 | + if (!snapshot.exists()) return []; |
| 50 | + const value = snapshot.val(); |
| 51 | + const courses = []; |
| 52 | + for (const id of Object.keys(courses)) { |
| 53 | + courses = [...courses, courses[id]]; |
| 54 | + } |
| 55 | + return courses; |
| 56 | +} |
0 commit comments