Next.js Discord

Discord Forum

Passing Parameters to all pages in Nested Routes (auth strategy)

Answered
Forest bachac posted this in #help-forum
Open in Discord
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?
Answered by B33fb0n3
@Forest bachac solved?
View full answer

15 Replies

Forest bachacOP
Or is there a way to pass the user from the layout to the page
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
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.tsx
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.
@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.
as of now i just get the user in each subpage but i was wondering if there was a more efficient page
@Forest bachac solved?
Answer