Next.js Discord

Discord Forum

Initial state loading

Answered
Philippine Crocodile posted this in #help-forum
Open in Discord
Philippine CrocodileOP
I make a simple app with posts. I have my own backend. How to implement initial fetching information about current user, like sending request for validating jwt. I think making it in every component is a bad idea. as a state manager I use zustand. I think it's a good practice to store user object in zustand store, isn't it?
Answered by B33fb0n3
ah, you can do it in a middleware or in every comp.
View full answer

31 Replies

you can do that in server components, try to see the documentation on what you can do in server components
especially the data fetching article
https://nextjs.org/docs/app/building-your-application/data-fetching/patterns
@B33fb0n3 you can do that in server components, try to see the documentation on what you can do in server components especially the data fetching article https://nextjs.org/docs/app/building-your-application/data-fetching/patterns
Philippine CrocodileOP
I've seen it but I still don't understand how to send request for checking current user beacuse it requires jwt
where is currently the problem? To get the jwt? To auth the user? To create the login form? ...
@B33fb0n3 where is currently the problem? To get the jwt? To auth the user? To create the login form? ...
Philippine CrocodileOP
I get my user information by sending request that includes jwt. As I know I can't access jwt in server components
So I can't fetch current user in server componenet
Which library do you use to auth the user?
@B33fb0n3 Which library do you use to auth the user?
Philippine CrocodileOP
I have my own backend
So I do auth with http requests to it
ah ok. Set the important keys as cookie and then access the cookies on your server component and fetch the user serverside
and?
Philippine CrocodileOP
I want to do so
yea, you can do so
Philippine CrocodileOP
so what's the common way to do that with jwt in localstorage
exactly like I told you
Set the important keys as cookie and then access the cookies on your server component and fetch the user serverside
Philippine CrocodileOP
ok, thanks
But also I wanna know what's the best way to implement it without server components
without? 🤔
In nextjs? If you want to do everything without, that you should use react ^^
Philippine CrocodileOP
yep
and how do they implement it in pure react?
Philippine CrocodileOP
yes but where should I make this request
in every component?
to get the user or to auth your user?
Philippine CrocodileOP
to get it
that's what I was interested in
ah, you can do it in a middleware or in every comp.
Answer
Or maybe inside a layout as comp
Philippine CrocodileOP
ok, thanks
Please mark a message as solution to your problem. You can see how to do that here: https://nextjs-forum.com/post/1163105513492467843#message-1163105519003766945