Help how to create protected routes
Answered
Siamese Crocodile posted this in #help-forum
Siamese CrocodileOP
I am using the latest version of nextjs, in this app, I have many folders, all of them have page.tsx, I also created a context where I check if the user is admin or has another role, to protect those pages where he is only an admin allowed, do I have to go through my whole folder and check this?
Answered by B33fb0n3
you can either add it to each file and check there (each page.tsx) or if you want it easy: create a middleware and add a matcher that match the protected routes. Then check once inside your middleware and you protected everything 👍
4 Replies
Siamese CrocodileOP
BE is made full in express(don't ask why), my job is to do fe in next and i have a pb doing protected routes on fe..
@Siamese Crocodile I am using the latest version of nextjs, in this app, I have many folders, all of them have page.tsx, I also created a context where I check if the user is admin or has another role, to protect those pages where he is only an admin allowed, do I have to go through my whole folder and check this?
you can either add it to each file and check there (each page.tsx) or if you want it easy: create a middleware and add a matcher that match the protected routes. Then check once inside your middleware and you protected everything 👍
Answer
Siamese CrocodileOP
thx
happy to help