X
X
Xbbv12016-10-19 12:25:49
React
Xbbv1, 2016-10-19 12:25:49

How to make authorization in react.js?

Hello.
I do authorization, the general scheme is as follows: there is an onEnter callback on closed pages in routers, in this callback I check the value from the Logged in? = true, No? = false.
If not logged in, I redirect to the login page. I bind the inputs of the login form to the values ​​of the store and then send them to the api for verification.
Now I want to set cookies and the question arose, where is the best place to check if the user is logged in? Put an onEnter callback on the root route / ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2016-10-19
@Xbbv1

Quite a good solution is to check permissions in onEnter. And why did you need cookies (with regards to authorization)? Tokens are best stored in localstorage and synchronized with your store.
Group the routes that require authorization and put a callback there:

<Route path="/" component={App}>
  <Route onEnter={checkAuth}>{/* Auth routes */}</Route>
  {/* Home page route */}
  <IndexRoute component={Home}>
  {/* Other public routes /*}
  <Route path="about" component={AboutPage} />
</Route>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question