Where should I put supabase.auth.onAuthStateChange?
Hi,
I'm trying to implement
supabase.auth.onAuthStateChange
into the client since supabase does not recommend using getSession()
in the BE and getUser()
does not include the custom claims in the token.
To implement the subscription, is there any recommended pattern? Right now I can think of
1) Using React Context - I already implemented this for ThemeProvider (shadcn). Then I wrapped the context around children in RootDocument.
Is there any other potential ways? I'm not sure how we can implement subscription in tanstack start router - i'm not sure if we can implement like React Context/Hooks in RouterRouter Context | TanStack Router React Docs
TanStack Router's router context is a very powerful tool that can be used for dependency injection among many other things. Aptly named, the router context is passed through the router and down throug...
19 Replies
adverse-sapphireOP•2mo ago
I'm asking this because I would like to use the user/session info inside
beforeload
so I can redirect user if they are not authenticated. With react context, I'm not sure how to implement it.
I think if I don't use react context, then I will use tanstack query + fetch session so at least i can cache the infodeep-jade•2mo ago
You can add it inside main.tsx, and pass it as context to the router provider
But this would be ok only for frontend. Like, for react router. Not sure if it will be ok for start
adverse-sapphireOP•2mo ago
I found a way to wrap ReactContext around. It is sorta like what Mihai said above. What I did:
- Create
client.tsx
so I can override the client entry - doc
- create normal React Context stuff like useAuth
and AuthProvider
Note: This is my current router setup ( i disabled ssr)
Learn the Basics | TanStack Start React Docs
This guide will help you learn the basics behind how TanStack Start works, regardless of how you set up your project. Dependencies TanStack Start is powered by and . TanStack Router: A router for buil...
deep-jade•2mo ago
Yea. Exactly like that!
adverse-sapphireOP•2mo ago
But it looks like im having HTML rendered mismatch which points to the RouterProvider
My AuthProvider
maybe because I try to override the user when thing loads 🤔
equal-aqua•2mo ago
you need to use
<StartClient>
otherwise router wont hydrate
but it should still be possible to implement this if you wrap both server and client entrypoints with the react provideradverse-sapphireOP•2mo ago
I have defaultSsr:false tho, does that mean I wouldn't need server entrypoints?
equal-aqua•2mo ago
see other thread
please use one thread
adverse-sapphireOP•2mo ago
I'll ask question on this one since the code snippet is above
you cannot just render "nothing" on the serverCould you give an example of how server entrypoint file may look like with React provider?
equal-aqua•2mo ago
this is the default
packages/react-start-server/src/defaultStreamHandler.tsx
you would just wrap StartServer
however, it does not allow passing in context yet
but since StartServer is just this
you could just use
RouterProvider
instead for now
to experiment with itadverse-sapphireOP•2mo ago
Gotcha, thank you! I'll use
RouterProvider
to experiment for now and hopefully solve the rendered HTML mismatch
Are we planning to allow passing context into <Start*/>
?equal-aqua•2mo ago
maybe? depends on what you find out 😉
btw, on the client, you can just build you own startclient for now based on the actual impl
adverse-sapphireOP•2mo ago
Hahaha thank you! I never thought about looking at the packages to see the implementation and then tweak it. Lemme give it a shot!!
foreign-sapphire•2mo ago
I had hydration mismatch issue yesterday, after updating to the latest packages, I fixed it by adding react vite plugin outside of the tanstackstart plugin, and enabled the customreactplugin inside tanstackstart, the error was gone
maybe it might be helpful
adverse-sapphireOP•2mo ago
I tried both attempts but they have a problem on their own:
#1 attempted replacing StartServer like:
and client.tsx
->
It still has the hydration issue :(
#2 Implement custom StartClient with/without modifying the server entry:
--> Context updating takes too long to update, so when beforeLoad check was bypassed. Funny thing is, when i have the hydration issue, the context was provided fast enough so the beforeLoad was not bypassed.
For context, for authenticated routes what I did was:
which navigates user to login and in login check again if user isAuthenticated then renavigate them again. Without custom StartClient, this one works but ran into mismatch HTML rendered
Not sure which tanstack start version you are on, but I couldn't find the customReactPlugin on mine 😦 Mine is
adverse-sapphireOP•2mo ago
The order of the execution is like this

foreign-sapphire•2mo ago
maybe you should try updating the packages then, mine is like this:
adverse-sapphireOP•2mo ago
I didn't realise 1.127 is out
Updated the package and got rid of the React context/Start client etc -> no more issue
foreign-sapphire•2mo ago
I successfully implemented a context for my usecase, with custom start client, the catch is, on dev mode, even if my app is on spa mode, it is rendering things on the server, on first load, i think the loader functions and stuff,
here is my implementation:
here is the custom startClient component:
here is my client.tsx file:
and on __root.tsx file:
now i can do like this:
for your case, if your code is fully client side, i'd just use tanstack store, it works even in the beforelaod as you can see in my beforeload function, and in your top level root component, you can update it using useEffect or something