O
O
One of Us2020-05-07 00:11:11
JavaScript
One of Us, 2020-05-07 00:11:11

How to keep session in SPA React?

Good day.

For the first time I reached authorization on React. Please tell me how it should be organized. I use openAPI vkontakte. I created a component for the UserBox panel where there is a "Login via VK" button. By pressing it in the actionCreator, I make a request with the VK.Auth.login function, get a response, write the data to the store and render it in the UserBox component. How to keep the session, is it necessary to constantly send a request every time the main component or round is rendered and check whether it is logged in or not, or how should such a process be organized in general?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2020-05-07
@GreyCrew

A very common question.
In fact, the contact takes care of all authorization issues. You just have to send requests and properly process responses.
If in steps:
1) You are authorized - saved the token in the local redax store (or in the global state context if without redax)
2) Then send the token with each subsequent request.
3) If the server returns you an error related to the invalidity of the token, then do the authorization again, or an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question