Answer the question
In order to leave comments, you need to log in
React router authorization with private routes?
Good time.
In order to form a correct understanding, I decided to ask.
I'm making a small React app. Approaching the development of the authorization form, a question arose.
Is it necessary to use "private" routes in order to subsequently redirect the user, if it is possible to check whether the user is logged in, and based on this, compose the necessary routers.
Option without private routes:
const router = inject('user')(observer( ({user}) => {
return <BrowserRouter>{!user.isLogin ? <UnAuth /> : <Auth />}</BrowserRouter>
}));
const router = inject('user')(observer( ({user}) => {
return (<BrowserRouter>
<PrivateRoute access={user.isLogin} path="/home" redirect="/login" component={Component} />
<Route path="/login" component={FormLogin} />
</BrowserRouter>)
}));
Answer the question
In order to leave comments, you need to log in
Private routes will be much more convenient when there are more than two routes in your application.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question