I am trying to protect a route in web app using load function ,reading the value of currentUser
from auth
to see if the user is logged in but it is always null. So when I refresh the page I am kicked out.
import { auth } from "$lib/firebase";import { redirect } from "@sveltejs/kit";export const load = async () => { const user = auth.currentUser; if (!user) { throw redirect(302, '/login'); } return {};};
I tried:
- to use
onAuthStateChanged
instead and implement a listener. - to stop firebase from reinitialize in refreshes.
- implement a function with
await
to see if there is delay on vale update. - Enforcing firebase persistence mode.
- It's a +page.ts. And I tried changing the values of ssd and csr as well to know how rendering affect this.
The result was always the same. null
as currentUser
.