I have this <Switch>
setup in my app:
<Switch>
<Route exact path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/logout" component={Logout} />
<EnsureLoggedIn>
<Route exact path="/user" component={User} />
</EnsureLoggedIn>
<Route component={NotFound} />
</Switch>
How the EnsureLoggedIn
component works shouldn't be too important for my question, although I just want to mention that it does use the withRouter
HOC to access the match
, location
, and history
props for routing/redirecting purposes.
What I want to have happen is that the EnsureLoggedIn
component only triggers for routes matched within it. So in this case, I'd only want a visit to /user
to trigger it. Anything else should move on to the NotFound
route instead.
What I find happens instead, is that as soon as /
, /login
and /logout
aren't matched, EnsureLoggedIn
matches, and gets mounted and rendered, and nothing ever makes it to NotFound
.
How can I get the behavior that I want? Alternatively, am I going about this the wrong way, and should I "protect" my logged-in routes in an entirely different manner?