Passing Parameters to all pages in Nested Routes (auth strategy)
Answered
Forest bachac posted this in #help-forum
Forest bachacOP
Is it possible to pass a parameter to each page in a nested route from its layout?
Full question:
How can I implement authentication for a route and all its child routes (e.g., /auth/...) in Next.js? While checking in the layout makes it easier, accessing the authentication object in the actual pages becomes challenging.
Should I opt for checking authentication in each page individually, despite it being more convoluted?
Full question:
How can I implement authentication for a route and all its child routes (e.g., /auth/...) in Next.js? While checking in the layout makes it easier, accessing the authentication object in the actual pages becomes challenging.
Should I opt for checking authentication in each page individually, despite it being more convoluted?
15 Replies
Forest bachacOP
Or is there a way to pass the user from the layout to the page
@Forest bachac **Is it possible to pass a parameter to each page in a nested route from its layout?**
Full question:
How can I implement authentication for a route and all its child routes (e.g., /auth/...) in Next.js? While checking in the layout makes it easier, accessing the authentication object in the actual pages becomes challenging.
Should I opt for checking authentication in each page individually, despite it being more convoluted?
I recommend you checking the auth inside your middleware. Though the matcher you can mark specific route and also nested routes with less code. Checking in each page individually is also possible, but it's harder to maintain
The issue is that i am checking the validity of the cookie bc im using a no db approach to session, but the user object/database access cant be done in edge servers
@Forest bachac Thats why I want to do it in my layout
thing is, doing it in the layout is not secure. https://github.com/eric-burel/securing-rsc-layout-leak
@Forest bachac Thats why I want to do it in my layout
Nah, don’t use layout because of the issue joulev said. You want to fetch either in middleware or on page
Serbian Hound
that issue isn't really demonstrative of a project in the real world. if you check if the user has a valid session in layout - you're going to also get the current user in
page.tsxnot really.
say you have a
the logic of that page doesn't really require you to get the current user. but because of the issue above, it will be a problem if you don't perform the auth check inside the
the readme of the github repo above explains when it is acceptable and when it's not.
say you have a
/[username] page that defaults to public but can be set to private by the user.the logic of that page doesn't really require you to get the current user. but because of the issue above, it will be a problem if you don't perform the auth check inside the
/[username] page itself.the readme of the github repo above explains when it is acceptable and when it's not.
@joulev not really.
say you have a `/[username]` page that defaults to public but can be set to private by the user.
the logic of that page doesn't really require you to get the current user. but because of the issue above, it will be a problem if you don't perform the auth check inside the `/[username]` page itself.
the readme of the github repo above explains when it is acceptable and when it's not.
Forest bachacOP
i worded my question wrong.
i am checking authentication in middleware. i was wondering if it is possible to get the user in the layout and pass it to each page in that route to make it easier/convoluted. they are already authorized by middleware.
i am checking authentication in middleware. i was wondering if it is possible to get the user in the layout and pass it to each page in that route to make it easier/convoluted. they are already authorized by middleware.
as of now i just get the user in each subpage but i was wondering if there was a more efficient page
@Forest bachac i worded my question wrong.
i am checking authentication in middleware. i was wondering if it is possible to get the user in the layout and pass it to each page in that route to make it easier/convoluted. they are already authorized by middleware.
If you need to user data more than once, fetch it also more than once. Use a react cache to memorize the result for the request and you are good to go
@B33fb0n3 If you need to user data more than once, fetch it also more than once. Use a react cache to memorize the result for the request and you are good to go
Forest bachacOP
shouldn't it recheck it on every request in case it is changed
@Forest bachac shouldn't it recheck it on every request in case it is changed
It's the same request, so no
@Forest bachac solved?
Answer