Skip to content

Commit da86c19

Browse files
committed
Complete migration to firebase 9
1 parent b2312c8 commit da86c19

File tree

14 files changed

+65
-81
lines changed

14 files changed

+65
-81
lines changed

src/components/NavBar.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
NavLink
1010
} from 'sveltestrap/src';
1111
import authStore from '../stores/authStore';
12-
import firebase from 'firebase/compat/app';
12+
13+
import { getAuth, signOut } from 'firebase/auth';
1314
1415
let isOpen = false;
1516
1617
// handles the logout of
1718
async function logout() {
18-
await firebase.auth().signOut();
19+
signOut(getAuth());
1920
}
2021
</script>
2122

src/db.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import firebase from 'firebase/app';
2-
import { firestore } from './firestore';
1+
import db from '$lib/firebase/store';
2+
import firebaseApp from '$lib/firebase/app';
3+
import { getStorage, ref, getDownloadURL, uploadBytes } from 'firebase/storage';
4+
import { collection, serverTimestamp, addDoc, updateDoc } from 'firebase/firestore';
5+
6+
import type { Timestamp, FieldValue } from 'firebase/firestore';
37

48
export type Ingredient = {
59
name: string;
@@ -11,8 +15,8 @@ export type Recipe = {
1115
title: string;
1216
description: string;
1317
userId: string;
14-
createdAt: firebase.firestore.Timestamp | firebase.firestore.FieldValue;
15-
updatedAt: firebase.firestore.Timestamp | firebase.firestore.FieldValue;
18+
createdAt: Timestamp | FieldValue;
19+
updatedAt: Timestamp | FieldValue;
1620
ingredients: Ingredient[];
1721
};
1822

@@ -29,27 +33,26 @@ export const createRecipe = async (recipeForm: RecipeForm, userId: string) => {
2933
description: recipeForm.description,
3034
ingredients: recipeForm.ingredients,
3135
userId,
32-
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
33-
updatedAt: firebase.firestore.FieldValue.serverTimestamp()
36+
createdAt: serverTimestamp(),
37+
updatedAt: serverTimestamp()
3438
};
35-
const db = await firestore();
36-
const recipeRef = await db.collection('recipes').add(recipe);
39+
const recipeRef = await addDoc(collection(db, 'recipes'), recipe);
3740
const path = await uploadFile(recipeRef.id, userId, recipeForm.mainPicture);
3841
const url = await getUrl(path);
39-
recipeRef.update('picture', url);
42+
await updateDoc(recipeRef, { picture: url });
4043

4144
return recipeRef;
4245
};
4346

4447
const uploadFile = async (recipeId: string, userId: string, pic: File) => {
4548
const mainPicturePath = `/${userId}/${recipeId}.${pic.name.split('.').pop()}`;
46-
const storage = firebase.storage();
47-
const ref = storage.ref(mainPicturePath);
48-
await ref.put(pic);
49+
const storage = getStorage(firebaseApp);
50+
const picref = ref(storage, mainPicturePath);
51+
await uploadBytes(picref, pic);
4952
return mainPicturePath;
5053
};
5154

5255
export const getUrl = async (path: string) => {
53-
const storage = firebase.storage();
54-
return await storage.ref(path).getDownloadURL();
56+
const storage = getStorage(firebaseApp);
57+
return await getDownloadURL(ref(storage, path));
5558
};

src/firestore.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/initFirebase.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/lib/firebase/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { initializeApp } from 'firebase/app';
2+
import firebaseConfig from '$lib/firebase/config';
3+
4+
const fb = initializeApp(firebaseConfig);
5+
export default fb;

src/lib/firebase/auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getAuth } from 'firebase/auth';
2+
import firebaseApp from '$lib/firebase/app';
3+
4+
const auth = getAuth(firebaseApp);
5+
6+
export default auth;
File renamed without changes.

src/lib/firebase/store.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import firebaseApp from '$lib/firebase/app';
2+
import { getFirestore } from 'firebase/firestore';
3+
4+
const db = getFirestore(firebaseApp);
5+
6+
export default db;

src/routes/+layout.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
<script context="module" lang="ts">
2-
</script>
3-
41
<script lang="ts">
52
import { Container } from 'sveltestrap';
6-
import firebase from 'firebase/compat/app';
7-
import 'firebase/compat/auth';
83
import { onMount } from 'svelte';
94
import authStore from '../stores/authStore';
105
import NavBar from '../components/NavBar.svelte';
6+
import { onAuthStateChanged } from 'firebase/auth';
7+
8+
import auth from '$lib/firebase/auth';
119
1210
onMount(() => {
13-
firebase.auth().onAuthStateChanged((user) => {
11+
onAuthStateChanged(auth, (user) => {
1412
authStore.set({
1513
isLoggedIn: user !== null,
1614
user,

src/routes/+layout.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { initFirebase } from '../initFirebase';
1+
import firebaseApp from '$lib/firebase/app';
22
import type { LayoutLoad } from './$types';
33

44
export const load: LayoutLoad = async () => {
5-
initFirebase();
6-
return {};
5+
return {
6+
app: firebaseApp
7+
};
78
};

0 commit comments

Comments
 (0)