-
Hello. I don't know that much about routing. Can anyone give me an idea how I can make protected routes? For example, a user has to be authenticated first before they can go to specific route. Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I guess it is similar to how you would implement protected routes with any other router. Generally, what you need is:
const RequireAuth(props) => {
const [location] = useLocation();
const user = useCurrentUser();
const isProtectedRoute = ... // assert that `location` matches the protected area, you can compare strings, use regexps or use `useRoute` from wouter
if (isProtectedRoute && !user) {
return <Redirect to="/auth" />;
}
// render protected routes
} |
Beta Was this translation helpful? Give feedback.
-
Hello again. Is there anyway I can make this work?
It seems the nested Route doesn't get to run. |
Beta Was this translation helpful? Give feedback.
-
Could you add a bit more context on what you're trying to achieve? |
Beta Was this translation helpful? Give feedback.
I guess it is similar to how you would implement protected routes with any other router. Generally, what you need is:
<ProtectedRoute>...</ProtectedRoute>
that will (pseudo-code):