How to store user data?
Unanswered
Sweat bee posted this in #help-forum
Sweat beeOP
I am using Supabase SSR for authentication. When the user has been authenticated, I want to fetch some extra data like name, profile picture, permissions etc. from an external API.
What would be the best solution to store this user info, so I can access it in server and client components?
In a client only app I would store it in a global state, but I guess that won't work on the server.
What would be the best solution to store this user info, so I can access it in server and client components?
In a client only app I would store it in a global state, but I guess that won't work on the server.
4 Replies
I would say having a utility function that does the user data fetching and wrapping it in
This way you will only ever hit the data spruce once and the following calls will get the data back from the cache. You will have to tell Next.js when to revalidate the data so it fetches fresh data again with
unstable_cache
This way you will only ever hit the data spruce once and the following calls will get the data back from the cache. You will have to tell Next.js when to revalidate the data so it fetches fresh data again with
revalidateTag()
: let’s say when user logs out, out session expired, etc.And yea you’re guessing correctly, the server doesn’t have such thing like Context or global state, the closest would be storing data in cookies but that’s not the best approach, works for simple cases tho.
The best would be to have an hybrid approach, where Clients Components can still access your data via a Global State or Context provider, and Server Components will call the fetcher function directly.
So let’s say you will fetch the user at the very top of your tree of components, you can put that user data in a global state (like context or whatever) for Client Components to access to it via Hooks. And for Server Components just call the
The best would be to have an hybrid approach, where Clients Components can still access your data via a Global State or Context provider, and Server Components will call the fetcher function directly.
So let’s say you will fetch the user at the very top of your tree of components, you can put that user data in a global state (like context or whatever) for Client Components to access to it via Hooks. And for Server Components just call the
getUserData()
that’s gonna be cached if you wrapped it in ‘unstable_cache’@Sweat bee I am using Supabase SSR for authentication. When the user has been authenticated, I want to fetch some extra data like name, profile picture, permissions etc. from an external API.
What would be the best solution to store this user info, so I can access it in server and client components?
In a client only app I would store it in a global state, but I guess that won't work on the server.
quick answer: [JWT](https://jwt.io)
It allows you to make a client server token and using it is easy:)
It allows you to make a client server token and using it is easy:)
you can also create a global client context to handle it in client side and JWT cookie for server side