Protect Routes
Answered
Barbary Lion posted this in #help-forum
Barbary LionOP
What is the best and most recommended way if I want to protect certain pages so that they can only be accessed when the user is logged in?
I know these ways:
- middleware
- wrap the page in a HOC
- wrapping layout in a provider
I know these ways:
- middleware
- wrap the page in a HOC
- wrapping layout in a provider
Answered by Plague
middleware as first line of defense for quick redirects, then page-level checks. Avoid layout checks.
If any data on those protected pages, you also want protected make sure to do checks in the data layer as well to make sure only logged in users can access that data.
If any data on those protected pages, you also want protected make sure to do checks in the data layer as well to make sure only logged in users can access that data.
4 Replies
middleware as first line of defense for quick redirects, then page-level checks. Avoid layout checks.
If any data on those protected pages, you also want protected make sure to do checks in the data layer as well to make sure only logged in users can access that data.
If any data on those protected pages, you also want protected make sure to do checks in the data layer as well to make sure only logged in users can access that data.
Answer
@Plague middleware as first line of defense for quick redirects, then page-level checks. Avoid layout checks.
If any data on those protected pages, you also want protected make sure to do checks in the data layer as well to make sure only logged in users can access that data.
Exactly, this.
Do not make auth checks in Layouts. And use middleware as an early return or a router, and if you put session fetch logic on there make sure it’s not taking time because you’re gonna make the app feel slow.
Do not make auth checks in Layouts. And use middleware as an early return or a router, and if you put session fetch logic on there make sure it’s not taking time because you’re gonna make the app feel slow.
Giant panda
@Plague @LuisLl I was also using middleware but then ran into this: https://nextjs-forum.com/post/1393520231846248590
Barbary LionOP
Alright, thanks a lot guys!