Router context not defined by provider during loader/beforeLoad?
I'm setting up my router and wrapping my providers with Wrap:
When I print the context in the first layout, auth is still undefined:
I am just setting and returning the provider with
isAuthenticated: true
for testing but it seems the provider never loads before loader. How come? According to the authentication examples, this should be working?5 Replies
harsh-harlequin•3w ago
Hi
Do you inject value from the hook into your router into Router Context ?
optimistic-goldOP•2w ago
I am doing it when AuthProvider is getting declared/initialized. This might not be enough? What are common patterns to get better auth working as part of the context with server functions?
Since authClient isn't retrieving cookies when server functions trigger, we won't get the session.
What's the best practice here? I want to ensure that the experience is fast without authed pages flickering.
I'm almost thinking if I should rather use start's middleware to retrieve and handle session while doing api/rpc calls directly using tanstack query.
harsh-harlequin•2w ago
I think if you’re planning to bring TanStack Query into the picture, you don’t really need the router context anymore.
You could also use the server function in beforeLoad on __root and then return the data to update the router context afterward.
Also, I still don’t quite understand why you even need the React context in the first place.
optimistic-goldOP•2w ago
I was thinking convenience (also new to tsr) but perhaps it’s just unnecessar?
That said, the goal is to have consistent auth, accessible in authed layout, with as little loading as possible to the end user.
harsh-harlequin•2w ago
If it were up to me, I’d just leave everything to the TanStack Query,
and in beforeLoad, I’d ensure the data for the user.