-
I'm working with React Router v6.30.1 (createBrowserRouter) and would like to centralize authentication and authorization logic at the layout level, rather than duplicating it across each route’s loader. My goal is to restrict access to certain parts of the URL hierarchy (e.g., the <Route path="admin" element={<AdminLayout />} loader={authNLoader}>
<Route index element={<SectionA />} loader={aDataLoader} />
<Route path="b" element={<SectionB />} loader={bDataLoader} />
<Route path="b/:id" element={<ItemB />} loader={bItemsDataLoader} />
<Route path="c" element={<SectionCLayout />} loader={authZLoader}>
<Route path="c1" element={<SubSectionC1 />} loader={c1DataLoader} />
<Route path="c2" element={<SubSectionC2 />} loader={c2DataLoader} />
<Route path="c3" element={<SubSectionC3 />} loader={c3DataLoader} />
</Route>
</Route> - Is this a recommended pattern in React Router 6? Are there any caveats or edge cases I should be aware of when centralizing authentication and authorization in layout loaders like this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
This is wrong, parent and child loaders are all called in parallel You need to add authorization to every loader as you don't know which will will resolve it faster. |
Beta Was this translation helpful? Give feedback.
This is wrong, parent and child loaders are all called in parallel
You need to add authorization to every loader as you don't know which will will resolve it faster.