Next.js Discord

Discord Forum

How to pass data from middleware to layouts/pages?

Answered
Dogue Brasileiro posted this in #help-forum
Open in Discord
Dogue BrasileiroOP
I am refreshing jwt token inside my middleware and redirecting based on the login session. I am also getting the user data if the user is logged in. I want to share this data from middleware to layouts and pages. How do i do that?
Answered by Arinji
its out of the 3

pass as cookies
pass as headers
pass as url
View full answer

56 Replies

put the data in cookies
then get the cookies from the layouts/pages
cookies().set
cookies().get
@Arinji put the data in cookies
Dogue BrasileiroOP
I dont think that's a good way to do this
i dont want to put all the user data into the cookies
I would rather pass it as a json object if theres a way
well thats the only way to do it, other than putting it in the url.....
Dogue BrasileiroOP
is there a reason why nextjs doesn't support this?
This seems like a common usecase
well you could pass as headers as well
its out of the 3

pass as cookies
pass as headers
pass as url
Answer
@Dogue Brasileiro is there a reason why nextjs doesn't support this?
i would think cause the middleware has no way of knowing where the request is from to pass props, but not sure
@Dogue Brasileiro is there a reason why nextjs doesn't support this?
because the middleware live in the edge network which is on different runtime
@Ray because the middleware live in the edge network which is on different runtime
Dogue BrasileiroOP
so you can't use edge middlewares for protecting your routes and getting user data?
just not passing the data
@Ray just not passing the data
Dogue BrasileiroOP
Yeah that's the problem. I have this external endpoint /users/me, which checks the access_token and returns user data.
Dogue BrasileiroOP
i need to fetch this endpoint everytime with access token to check if its valid or not
@Ray you could fetch the data again on your page
Dogue BrasileiroOP
But i would need to send 2 duplicate requests
I prefer to not duplicate the code
this endpoint is external api
if that case, you could use client side fetching instead
and don't use middleware
router cache is gonna give you headache
Dogue BrasileiroOP
But i would need auth ssr for faster page loads
what do you mean auth ssr for after page load?
Dogue BrasileiroOP
also i would need to access user data inside server components
and actions
@Ray what do you mean auth ssr for after page load?
Dogue BrasileiroOP
checking if the user is logged in when the page loads and redirecting
then you are gonna send 2 requests
Dogue BrasileiroOP
That seems like a bad code practice
why?
you can reuse the function
@Ray you can reuse the function
Dogue BrasileiroOP
You mean i can reuse function in middleware and client side
How do you do that?
@Dogue Brasileiro How do you do that?
just create a reusable function?
@Ray just create a reusable function?
Dogue BrasileiroOP
ok myb got confused
@Ray just create a reusable function?
Dogue BrasileiroOP
btw you can send request to next route endpoints inside the middleware Right?
Dogue BrasileiroOP
ok cool
set the data to request.headers if that value doesn't need to send to client
@Ray set the data to request.headers if that value doesn't need to send to client
Dogue BrasileiroOP
i dont understand
@Dogue Brasileiro i dont understand
if you set the data on request.header, it won't show on client browser
@Ray if you set the data on request.header, it won't show on client browser
Dogue BrasileiroOP
but why would you need to do that?
@Dogue Brasileiro but why would you need to do that?
because you just want to read the value on page?
and doesn't need to send to client?
Dogue BrasileiroOP
You mean from the middleware Right?
yeah
@Ray yeah
Dogue BrasileiroOP
doesn't it show the data inside the console ?
what console?
@Ray what console?
Dogue BrasileiroOP
browser console networks tab
why would the data inside middleware show on browser console
no if you set in request object
yes if you set in response
Dogue BrasileiroOP
Yeah i think i would need to set in response to get in the pages i think