V
V
VIGF2019-05-03 17:11:52
JavaScript
VIGF, 2019-05-03 17:11:52

How to do authorization in React without Redux?

It is necessary to implement on the front (React) user login/registration without using Redux. What data should be stored and where?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-05-03
@VIGF

On the example of using tokens.
Authorization:
1. We send a pair of login / password, in case of success we receive a token and user data in the response.
2. We write the user's data to the state of the main component, from it we throw it into the context.
3. We write the token in the cookie and in the default headers of the library for http requests.
4. Authorization and logout callbacks can also be thrown into the login form and the logout button through the context.
Application initialization:
1. Check the cookie for the presence of a token.
2. If it is, we request the user and write to the main component.
3. We write the token in the default headers of the library for http requests.
logout:
1. Remove the token from the cookie and default headers of the library for http requests.
2. Remove user data from the state of the main component.
3. We make a redirect from a secure page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question