Private routing #10327
-
I am using this: (which I believe is a newer feature)
I need to restrict access to some routes and I tried doing it by creating this component:
However, I get the following error: How should I got about restricting access to some routes? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
// utility function you can use in any loader that requires authorization
async function requireUser(request) {
let user = await getUser(request);
if (!user) {
throw redirect('/login');
}
}
async function authorizedRouteLoader({ request }) {
await requireUser(request); // throw redirect if not logged in
// ... load data normally here
} At the moment you'll want to perform that check in every authorized route loader since they all run in parallel, but as soon as the proposed middleware API lands you'll be able to do this in a single spot in the code |
Beta Was this translation helpful? Give feedback.
RouterProvider
decouples data fetching from rendering, so the idiomatic place to be doing auth-detection and redirection is in your route loaders:At the moment you'll want to perform that check in every authorized route loader since they all run in parallel, but as soon as the proposed middleware API lands you'll be able to do this in a …