Invalidating router context cache with supabase integration
Hello, I face a problem to reinvalidate router context when going to login page via URL. The issue is caused by lack of reinvalidation of react router context. In my setup I have an Auth context provider with effect
Sadly here I cannot revalidate context after setting the user state - this provider lives outside of router provider.
Do you know how possibly I could make the context revalide after the user session is checked so that the Login component is not rendered?
6 Replies
flat-fuchsiaOP•2mo ago
My Router Provider setup looks like following?
magic-amber•2mo ago
Sadly here I cannot revalidate context after setting the user state - this provider lives outside of router provider.why is that?
flat-fuchsiaOP•2mo ago
I cannot use
useRouter hook in the provider as the router provider does not exist
flat-fuchsiaOP•2mo ago
I found this post - https://github.com/TanStack/router/discussions/2978 - where you instruct to use router from createRouter directly, not via the useRouter hook. Is it a solution?
GitHub
Warning: useRouter must be used inside a <RouterProvider> component...
I am creating an authenticated route using the TanStack Router documentation. According to the docs, I need to create an inner component like this: function InnerApp() { const auth = useAuth(); ret...
magic-amber•2mo ago
if you have a circular provider issue, then yes this helps
sometimes you can also rearrange the provider hierachy
flat-fuchsiaOP•2mo ago
Maybe InnerWrap property will be a better solution
Or some effect to invalidate the router context when authContext change
Ideally I wouldn't like the AuthProvider need to handle router context invalidation. I would prefer the router context to automatically detect state changes and revalidate
This option also works:
And SupabaseAuthProvider is not dependent on router directly, which is cleaner.