Basic app architecture question for Auth/session state retrieval & storage
This is my first larger full-stack app so please bear with me 😄
I have a basic question regarding how to handle authentication state throughout the app and different components. So my understanding is, that to some extent you don't actually need global state management since you can rely on the hooks like
useSession
etc. to sync your frontend with whether a user is authenticated or not / get user data. But I have some parts of user data that is stored in another table, which I need to query with tRPC / Prisma separately (user profile data eg. full name, avatar pic url, etc).
My idea was to create a global store using Zustand and set that data there once a session is created. From then on, retrieve it only from there, throughout the app and all components that need that data. Then have some invalidation logic to clear the store data when the sessions ends.
My reasoning behind this approach using a global store for user data would be that this way there would be no need to fetch the same piece of data multiple times and passing it around as props would lead to some deep prop drilling as the app grows.
Now my question is, is there a better, more best-practice way of doing this? Am I missing something crucial?
Thanks a lot17 Replies
My first implementation looks like this:
The store:
I'm pretty sure it's
create((set, get) => ({}))
Not create()((set) => ({}))
But I would also say you don't need a zustand store to store the user?GitHub
GitHub - supabase/auth-helpers: A collection of framework specific ...
A collection of framework specific Auth utilities for working with Supabase. - GitHub - supabase/auth-helpers: A collection of framework specific Auth utilities for working with Supabase.
Sorry, I was abstracting away the TS code and did an error. Code is corrected now
It's still not
create()((set, get) => ({}))
My point is I'm storing additional user data somewhere else, in a different table whose data is not exposed by any auth helper method
https://github.com/pmndrs/zustand/blob/main/docs/guides/typescript.md#basic-usage
Ah there's a different way for Typescript lol didn't know that
Ohh wait
You can't use hooks inside random functions
This is a no no
Only hooks and components can use hooks
If you just copy pasted the contents of that function inside of App is it working
Wait you're returning null and it's a component bro what is going on in this code lol
api.profiles.getById.useQuery({ id: session?.user.id })
What is this? You have a whole seperate database?it's a renderless component
no. just a different table
And if you don't do that and just put this inside of App.tsx
Would it be working
no because it's outside of supabase
<SessionContextProvider>
. That's why I use the renderless <AuthStateGrabber>
componentAh
Can u please check my problem?
Maybe that works?
I already did, spend some time going through the codebase, I don't know what's going wrong, please don't go into other people's questions just to ask for help
@barry I solved the infinite loop problem using
useEffect
see my updated original question. Now the question is just about the pattern really. Whether this is a common pattern and if there's a better practice of handling such caseWell what I just showed you lol
Should work, we've got a query that only runs if session.user.id exists
When that query is successful it takes said response and sets the profile to it
uk I already wasted 3-4 h on the problem I am waiting for other people reply
A lot of people are from the US here, it's like, 4 AM at most there, and in most of western Europe it's barely 12:00 (morning).