S
S
slanec2019-04-29 00:30:27
User identification
slanec, 2019-04-29 00:30:27

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>
     }));

Option with private routes:
const router = inject('user')(observer( ({user}) => {
          return (<BrowserRouter>
                <PrivateRoute access={user.isLogin} path="/home" redirect="/login" component={Component} />
                <Route path="/login" component={FormLogin} />
          </BrowserRouter>)
     }));

What and when is better and what is generally more correct to use? totally confused...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-04-29
@rockon404

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 question

Ask a Question

731 491 924 answers to any question