Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Created March 18, 2021 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoyfellow/2779fb121fac0f32c7419e8dfede772d to your computer and use it in GitHub Desktop.
Save acoyfellow/2779fb121fac0f32c7419e8dfede772d to your computer and use it in GitHub Desktop.
Example of how to initialize firebase@exp
import { initializeApp } from 'firebase/app';
import { getAuth, onAuthStateChanged } from "firebase/auth";
import { getFirestore } from 'firebase/firestore';
import { getStorage } from "firebase/storage";
let ssrAuthed = window.ssrUser;
const firebaseConfig = {
// ...
};
const firebase = window.firebase || initializeApp(firebaseConfig);
window.firebase = firebase;
const auth = getAuth(firebase);
const firestore = getFirestore(firebase);
const storage = getStorage(firebase);
onAuthStateChanged(auth, (u) => {
console.log({ ssrAuthed, u });
});
export {
firebase,
auth,
firestore,
storage
};
@thormuller
Copy link

thormuller commented Apr 17, 2021

Hi Jordan, thanks again for posting these SvelteKit/Firebase snippets. I've found them helpful. One thing isn't clear to me is how you handle maintaining sessions between client and server with SSR, though I may just have missed the logic. I see you have that ssrAuthed variable, but how are you using this in the course of loading SSR pages? Or how are you using hooks in your requests? Or is there another approach you're taking?

@acoyfellow
Copy link
Author

acoyfellow commented Apr 17, 2021

@thormuller, you're correct, i use hooks

example:

and the ssrAuthed varible gets set from my $layout.svelte file, like this:

Then, in every child route or component, i can access anything from that firebaseClient.js:

import { getContext } from 'svelte';
const store = getContext('store');
// $store.firebase, $store.firestore, etc..

@thormuller
Copy link

Thanks so much for the detail. Seeing the hooks doc in particular was helpful. All the best!

@thormuller
Copy link

One other question: did you have any issues in build mode? I'm getting the old window is not defined error when I run build, but not in dev.

@acoyfellow
Copy link
Author

acoyfellow commented May 5, 2021

@thormuller - no i don't.. but, does that error persist if you add a browser check in the $layout.svelte? Just to be sure this file isn't called on the server. I wonder if that would help.

It may depend on the versio of sk, there might be a change I didn't catch up to yet

@JakubBlaha
Copy link

@thormuller Instead of using the window object to save the app instance, I have used this and it seems to work.

import { initializeApp, getApp, getApps } from 'firebase/app';

const firebase = (getApps().length && getApp()) || initializeApp(firebaseConfig);

@thormuller
Copy link

Thank you both. To @acoyfellow's point, there was just a bit update to SK and an update to Vite. I'm in the midst of that update, so perhaps this is an artifact of the swirl. I'll definitely try your solution if the update doesn't fix things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment