I am building a website with SvelteKit. My auth provider is Firebase. I am trying to protect some pages with Authentication, so I am programming an auth listener in __layout.svelte. However, no matter how I try, I can't seems to pass the Auth Status from the context module to my script, this is my code:
__layout.svelte
<script lang="ts" context="module">
import type { User } from 'firebase/auth';
import { onAuthStateChanged } from "firebase/auth";
import { auth } from "$lib/scripts/firebaseStart";
export async function load() {
let authUser: User|boolean = await getAuthUser();
async function getAuthUser() {
await onAuthStateChanged(auth, (user) => {
// let authUser: User|undefined;
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
authUser = user;
console.log(authUser); // logs authUser out as I expected it to
// ...
} else {
// User is signed out
// ...
authUser = false;
}
});
return authUser;
}
return {
props: { authUser }
};
}
</script>
<script lang="ts">
export let authUser: User|boolean;
console.log(authUser); // logs undefined and executed before the above console log
</script>
The conclusion I get is that the code in script tag is executed before the context=module could return the props. How do I make the code in the context module 'blocking'?