Vue Pinia Firebase Authentication Fetch currentUser before Route Guard
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In a Vue app that uses Firebase Authentication, currentUser is not reliably available the moment the page reloads. Firebase restores the session asynchronously, so a route guard that checks too early can redirect an authenticated user to the login page before the auth state has finished initializing.
Wait for Firebase Auth Exactly Once
The clean pattern is to let your Pinia auth store own the initialization promise. The store waits for the first onAuthStateChanged callback, saves the user, and marks itself ready so the rest of the app can await that single piece of state.
This avoids the common mistake of subscribing in every guard call. You only wait for the initial auth restoration once, then read store state after that.
Await the Store Before the Route Check
With the store in place, the router guard becomes predictable. It waits for auth readiness and only then decides whether the route should continue.
This pattern removes flicker because the decision happens after Firebase has restored the previous session, not before.
Keep Long-Lived Auth State Separate From Startup Readiness
The first auth callback solves the startup problem, but your app may still need a persistent listener if the user can sign out, sign in on another tab, or refresh tokens while the app is open. A common pattern is to use one method for the one-time startup wait and another for a long-lived subscription that keeps the store synchronized.
That separation keeps the guard logic simple. The guard only cares about readiness and current user state, while the store remains responsible for ongoing auth events.
Avoid Reading currentUser Directly in the Guard
getAuth().currentUser can be null during initialization even when a valid session exists. Reading it directly in the guard often works on a warm app state and then fails on a hard refresh, which makes the bug seem random.
Treat the store as the source of truth after initialization. That gives you one consistent place to handle loading, user state, and any fallback redirect logic.
Common Pitfalls
- Checking
getAuth().currentUserbefore Firebase has finished restoring the session from storage. - Registering a new
onAuthStateChangedlistener on every route navigation and never cleaning it up. - Running the guard before Pinia is installed on the app instance, which makes the store unavailable in router code.
- Redirecting every unauthenticated request to login without excluding the login route itself, which can create a redirect loop.
- Mixing startup readiness and long-lived auth synchronization into one unclear method, which makes later maintenance harder.
Summary
- Firebase auth restoration is asynchronous, so guards must wait for readiness.
- Put the one-time initialization promise in the Pinia store.
- Await that store method inside
beforeEachbefore checking protected routes. - Use store state rather than reading
currentUserdirectly during startup. - Separate initial auth loading from long-lived auth subscriptions for clearer code.
Related reading
- Vulnerability in RabbitMQ disable cleartext authentication mechanisms in the amqp configuration
- Waiting for HTTP-01 challenge propagation wrong status code ''404'', expected ''200''
- WARN Failed to send SSL Close message(Kafka SSL configuration issue)
- Warning about SSL connection when connecting to MySQL database
- Wait for all different promise to finish nodejs async await
- Wait for async method in for loop in Cypress
- Warning the user/local/mysql/data directory is not owned by the mysql user
- WARNING UNPROTECTED PRIVATE KEY FILE when trying to SSH into Amazon EC2 Instance

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.