Next.js Discord

Discord Forum

Access req (NextRequest) in layout/page.tsx files

Unanswered
Cane Corso posted this in #help-forum
Open in Discord
Cane CorsoOP
Hey, I want to make a page that only accessible with the admin role, in the pages version i could simply use getServerSideProps but the app directory doesnt have that option.
how can i access the req property?
I saw i can use the middleware file, but it seems bad to make just one file for that in the base directory just for one route.

3 Replies

Cane CorsoOP
The reason im not using the headers and cookies is because im accessing the user object through nextConnect & session:
await router.run(req, res
req.user // the user object
You answered yourself. You can’t get the request object within a normal server component. Only the cookies and headers. The only and normally best way is to use a middleware to access that
If that helps you, than please mark my message as solution. You can see how to do that here: https://nextjs-forum.com/post/1163585002605391932#message-1163585012671725760