There are some ways to solve this friend, let’s go in pieces.
Requisitions
The Reactjs is only a library for building interface, does not offer you, as well as frameworks native request ways, you can use other libraries like Xios, or even use the api fetch present in ES6, or even the old Xmlhttprequest.
Example with Axios:
import React from 'react';
import axios from 'axios';
export default class PersonList extends React.Component {
state = {
persons: []
}
componentDidMount() {
axios.get(`https://jsonplaceholder.typicode.com/users`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
}
render() {
return (
<ul>
{ this.state.persons.map(person => <li>{person.name}</li>)}
</ul>
)
}
}
Example with fetch:
import React from 'react';
import axios from 'axios';
export default class PersonList extends React.Component {
state = {
persons: []
}
componentDidMount() {
fetch(`https://jsonplaceholder.typicode.com/users`)
.then((resp) => resp.json())
.then(function(data) {
const persons = res.data;
this.setState({ persons });
})
})
}
render() {
return (
<ul>
{ this.state.persons.map(person => <li>{person.name}</li>)}
</ul>
)
}
}
Source
Sessions
Ways to keep the user logged in
This will depend more on your architecture, nowadays there is a larger trend for using JWT’s (JSON Web Token), where you store a token generated in your application backend and sends them as header in requests for routes that are protected, (Basically a WEB badge system).
This token can be stored through the API Webstorage, sessionStorage
or localStorage
.
To maintain security it is always important to take a look at how to store these tokens safely by implementing synchronization tokens for example, I advise to take a read on this article https://auth0.com/docs/security/store-tokens and in that https://medium.com/tableless/entendendo-tokens-jwt-json-web-token-413c6d1397f6.
You can vary the implementation type with other libraries
, we also have the redux-react-session
, in which case it will depend on your environment.
The reactjs is very agnostic of this kind of thing, you can search and test the best strategy that fits what you need.
Opa @Mslacerda, thank you very much for the reply. Come on, I’ve already created token authentication, using JWT until then beauty, my problem is this, if I use Xios or fetch I have some errors, arising from CORS, however if I do the request using Node works normal. However, if I for example saw that the user is who he claims to be, every page he goes to I have to check again? This leaves me in doubt, because with Session you store that login made by him
– Anderson Henrique
What’s the mistake? Can you put it in your question? You can check if someone is logged in and you can also check the authenticity of that token in question whenever the user enters a new page or makes a request will depend on how secure you want your application to be.
– MSLacerda
The error I already have a question asked here, https://answall.com/questions/334219/problema-em-calling-de-api-no-react-js, I’m going to add an image I think it’s going to be clearer what I’m saying
– Anderson Henrique