Skip to content

Private routing #10327

Closed Answered by brophdawg11
jotaachm asked this question in Q&A
Apr 9, 2023 · 1 comments · 9 replies
Discussion options

You must be logged in to vote

RouterProvider decouples data fetching from rendering, so the idiomatic place to be doing auth-detection and redirection is in your route loaders:

// 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 …

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@jotaachm
Comment options

@danieltkach
Comment options

@brophdawg11
Comment options

@danieltkach
Comment options

@brophdawg11
Comment options

Answer selected by MichaelDeBoey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants