This example demonstrates how to restrict access to routes to authenticated users.
Be sure to pay attention to the following features:
useNavigate()
hook and the <Navigate>
component for navigating both imperatively after the login form is submitted and declaratively when a non-authenticated user visits a particular routelocation.state
to preserve the previous location so you can send the user there after they authenticatenavigate("...", { replace: true })
to replace the /login
route in the history stack so the user doesn't return to the login page when clicking the back button after logging inOpen this example on StackBlitz: